In PHP website development, there are usually two ways to store data: one is to store text files, such as txt files, and the other is to store data in a database, such as Mysql, file storage has no advantages, but file read/write operations are still used in basic PHP development. Today I will share with you how to use PHP technology to implement file write operations for file read/write, it is also an introduction to PHP file read/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
Copy codeThe Code is as follows:
<?
@ $ Fp = fopen ("leapsoulcn.txt", "w ");
If (! $ Fp ){
Echo "system error ";
Exit ();
} Else {
$ FileData = "domain". "\ t". "www.leapsoul.cn". "\ n ";
$ FileData = $ fileData. "description". "\ t". "PHP tutorial network for PHP beginners. "." \ N ";
$ FileData = $ fileData. "title". "\ t". "This article describes the most basic file writing tutorial for PHP file read/write operations. ";
Fwrite ($ fp, $ fileData );
Fclose ($ fp );
}
?>
Note: In this file read/write instance code, the main function is to write two lines of text in the file.
Knowledge point:
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 codeThe Code is 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 is generally C: \, and Unix is/. You can also open the remote file through 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, line feed storage, mainly using \ n as the line feed separator.
The fwrite file writing function prototype is as follows:
Copy codeThe Code is 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.
Now the file write operation is complete.
The above is the most basic application of file write operations in PHP file read/write operations tutorial. In addition to file write operations, you often need to read the relevant file content during PHP website development, different functions can be used in file read/write operations. Next time, I will explain how to read files.