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. 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@ $ 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:
1
|
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:
1
|
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.