Overview of php file operations in php basic tutorial

WBOY
Release: 2016-07-25 08:57:15
Original
905 people have browsed it
Operating files in PHP is the most basic requirement for PHP programmers. PHP provides a large number of tools for creating, uploading and editing files.

This tutorial begins to explain how to interact with files. To help everyone understand all types of file operations in PHP!

Notes on php file operations

Be very careful when operating files in php. Common mistakes include editing the wrong files, filling your hard drive with junk data and accidentally deleting the contents of files.

In daily PHP development, file operations should be used reasonably and exception capture should be done to avoid unnecessary misoperations and even major file security failures as much as possible.

php file operation overview

To learn PHP file operations, you need to know how to create, open and close files. With these basic knowledge, we will learn the basic operations of files, including: reading, writing, appending, truncation, uploading files with PHP, etc.

Let’s start with the simplest part of php file operation and lead you to master the method of creating files in php.

Creation of PHP files

In this section, learn how to create a file using PHP.

In PHP, an interesting phenomenon is that you can use fopen to open and create files, and you can use this method.

Open files using fopen function in PHP. It can also create a file if it does not find the file specified in the function call. So, if you use fopen to open a file that doesn't exist, it will create it. In addition, when opening a file with fopen, you can write or append, which will be introduced later.

How to create a file in php The fopen function has two important parameters. First, a file name must be provided. Secondly, you must tell this method what operations it will perform, such as reading from a file, writing information, etc.

To create a file, you must provide a file name and tell PHP that you want to write to a file. Reference code:

Copy after login

Code description: Create the file testFile.txt in the current directory. The creation program detects that the file testFile.txt does not exist, and detects that the 'w' option is used in the method, so it creates the file.

$ourFileName = "testFile.txt"; Store the file to be created: testFile.txt into a PHP string variable $ourFileName.

$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); This code actually has two parts. First, use the open function of the fopen method and provide two parameters: the file name, and the wildcard character 'w', which means that if the file does not exist, create it in writing mode.

Let me tell you in advance what a file handle is. When the fopen function is used to operate a related file, a file handle will be returned. Here the file handle is saved to the $ourFileHandle variable. More information about file handles will be introduced in detail in future articles.

fclose($ourFileHandle); This sentence closes the open file. fclose receives one parameter, which is the file handle to be closed.

php file permissions Except for the writable parameter 'w' in the above fopen function, or the 'a' option to be discussed later. Before running PHP's file manipulation program, also check whether the PHP file has been granted permission to write information to the hard drive.

In Linux systems, use the chmod command to set permissions. Use chmod to allow PHP files to be written to the disk, allowing it to create a file.

This is the basic knowledge about php file operations. I hope it will be helpful to everyone.

For more tutorials on php file operations, please pay attention to the subsequent articles provided by Programmer’s Home.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!