During this time, I have been engaged in a lot of file operations and have no time to summarize them. Now I am a little idle. just summarize the process based on my own understanding.
1. Open a file
To open a file in PHP, you can use the fopen () function. At this time, we also need to specify how to use it, which is the file mode.
1.1 select file Mode
Essentially, the file mode tells the operating system a mechanism that determines how to handle access requests from other people or scripts, and a method to check whether you have the right to access this specific file.
When opening a file, you can choose from the following three options:
1) open the file to read-only, write-only, read-and-write.
2) If you want to write a file, you may want to overwrite all the existing file content, or simply append new data to the end of the file. If the file already exists, terminate the program execution instead of overwriting the file.
3) If you want to write a file on a system that differentiates binary and plain text, you must specify the method used.
The fopen () function supports the combination of the above three methods.
1.2 use fopen () to open a file
$ Fp = fopen ("$ document_root/../orders/orders.txt", 'w ');
When calling fopen (), two, three, or four parameters must be passed. Generally, two parameters are used.
Note: in UNIX, the delimiter in the directory is a forward slash (/). If you are using a Windows platform, you can use a forward or backward slash. If you use a backslash, you must use an escape character so that the function can correctly understand these characters. Generally, we still use a forward slash to run the code on Windows and Unix machines without any modification.
The second parameter of the fopen () function is the file mode, which is a string that specifies the operations on the file.
The file mode of the fopen () function is summarized as follows:
| Mode |
Mode name |
Meaning |
| R |
Read-Only |
Read mode-open a file and read it from the file header |
| R + |
Read-Only |
Read/write mode-open a file and read/write from the file header |
| W |
Write only |
Write mode-open the file and read it from the file header. If the file already exists, the existing content of all files will be deleted. If the file does not exist, the function will create the file. |
| W + |
Write only |
Write mode-open the file and start reading and writing from the file header. If the file already exists, the existing content of all files will be deleted. If the file does not exist, the function will create the file. |
| X |
Write with caution |
In write mode, open the file and start writing from the file header. If the file already exists, the file will not be opened, the fopen () function returns false, and PHP will generate a warning |
| X + |
Write with caution |
In write/read mode, open the file and start writing from the file header. If the file already exists, the file will not be opened, the fopen () function will return false, and PHP will generate a warning |
| A |
Append |
Append mode-open the file. If the file already contains content, it will be appended (written) from the end of the file. If the file does not exist, the function will create the file. |
| A + |
Append |
Append mode-open the file. If the file already contains content, it will be appended (written) or read from the end of the file. If the file does not exist, the function will create the file. |
| B |
Binary |
Binary Mode-used to connect to other modes. If the file system can distinguish between binary files and text files. We recommend that you always use this option for maximum portability. |
| T |
Text |
It is used in combination with other modes. Not recommended |
The 3rd parameters of the fopen () function are optional. If you want to search for a file in include_path, you can use it. If you want to perform this operation, you can set this parameter to 1.
The 4th parameters are also optional. fopen () function allows the file name to start with the protocol name and open the file remotely.
If fopen () successfully opens a file, the function returns a pointer to the file. We use this file pointer to operate this file.
2. Write files
In PHP, you can use fwrite () or fputs () to write files. The call method is as follows:
Fwrite ($ FP, $ outputstring );
This function tells PHP to write the string saved in $ outputstring to the file pointed to by $ FP.
2.1. fwrite () Parameters
The fwrite () prototype is as follows:
Int fwrite (resource handle, string [, int length])
The length parameter contains the maximum number of characters written.
2.2 File Format
3. close the file
Close the file after it is used. Call the fclose () function:
Fclose ($ FP );
If the file is successfully closed, the function returns a value of true. Otherwise, the function returns false.
4. Read files
4.1 open the file in read-only mode: fopen ()
Still use the fopen () function to open the file. Open the file in read-only mode, so the "rb" file mode is used.
4.2 know when to read the file: feof ()
Use the feof () function as the test condition for file termination:
While (! Feof ($ FP ))
The unique parameter of the function feof () is the file pointer. If the file Pointer Points to the end of the file, it returns true.
4.3 each time a row of data is read: fgets (), fgetss (), and fgetcsv ()
1) Use the fgets () function to read the file content: $ order = fgets ($ FP, 999 );
2) one variant of the fgets () function is the fgetss () function. Its prototype is string fgetss (resource FP, int length, string [allowable_tags]).
It can filter PHP and HTML tags contained in strings.
3) The fgetscv () function is another variant of fgets. Prototype: array fgetscv (resource FP, int length [, string delimiter [, string enclosure])
When the delimiters are used in the file, you can use this function to split the text into multiple lines.
4.4 read the entire file: readfile (), fpassthru () and file () 4.5, read one character: fgetc () 4.6, read any length: fread ()
Function prototype: String fread (resource FP, int length );
5. Use other useful file functions
5.1 check whether the file exists: file_exists () 5.2, determine the file size: filesize () 5.3, delete a file: unlink () 5.4, locate in the file: rewind (), fssek () and ftell ()
6. File Locking
In PHP, file locking is implemented through the flock () function. This function should be called before a file is opened and read/write operations are performed. The prototype of this function is as follows:
Bool flock (resource FP, int operation [, Int & wouldblock])
Note: Flock () functions cannot be used in NFS or other network file systems.