Php Tutorial file write operations
Writing data to a file involves three steps and some file operation functions:
1. Open the file (File operation function: fopen)
2. Write files (File operation functions: fwrite, etc)
3. Close the file (File operation function: fclose)
The following is an example of the file read/write operation code.
Basic PHP file write operation functions fopen, fwrite, fclose application tutorial
1. Use the fopen function to open a file. When applying the fopen function to open a file, you must first make it clear:
What do I do when I open a file? Do you want to read or write data into a file?
In addition, you need to consider whether to overwrite the data in the original file if the file already contains the relevant data, or simply add the new data to the end of the file.
These problems involve the application of the file mode in the fopen function in php file read/write operations. The fopen function prototype is as follows:
Copy the code as follows:
Fopen (filename, mode, include_path, context)
When calling the file operation function fopen (), two or three parameters are usually required.
Filename: specifies the file or url to be opened. You can specify the absolute path of the file. windows generally uses c:, unix as/. You can also open the remote file through the url. The file written here is in the same directory as the code file written to the PHP file.
Mode: specifies the access type of the file/stream. That is, the file opening mode.
Include_path: Optional. If you need to search for a file in include_path, you can set this parameter to 1 or true.
Description of common fopen file operation modes
"R"-open the file in read-only mode and read it from the file header.
"R +"-open a file in read/write mode.
"W"-open the file in writing mode and start writing from the file header. If the object does not exist, try to create it. If the object exists, delete the object content first.
"W +"-open the file in read/write mode and read and write from the file header. If the object does not exist, try to create it. If the object exists, delete the object content first.
"A"-open as a write, and append the write from the end of the file. If the file does not exist, try to create it.
"A +"-open in read/write mode and append write or read from the end of the file. If the file does not exist, try to create it.
Note: When performing file read/write operations, make sure that the opened file has the corresponding read/write permissions; otherwise, an error is returned for fopen. You can use @ to suppress the generated errors and handle them properly.
2. After opening a file using the file operation function fopen, you need to assign a value to the variable and write it to the file pointer pointed to by $ fp. In the above php file write operation tutorial instance, I use a row and a row storage, that is, a line feed storage, using n as the line feed separator.
The fwrite file writing function prototype is as follows:
Copy the code as follows:
Fwrite (fp, string, length)
You can also use the file writing function fputs, which is the alias function of fwrite. The Function and usage are the same as that of fwrite.
In the file writing function fwrite, length is optional. It is mainly used to set the maximum number of characters written to the file. If this parameter is set, fwrite will follow the set length, write the specified length of characters in the specified file. Fwrite () returns the number of characters written to the file. If an error occurs, false is returned.
After the file write operation is complete, you need to close the file handle, otherwise it will occupy system resources. You can use the fclose ($ fp) function to complete this task. If the object is successfully closed, true is returned; otherwise, false is returned.