PHP: File operation introduction: This is a PHP: File operation details page. it introduces php, related knowledge, skills, experience, and some php source code.
Class = 'pingjiaf' frameborder = '0' src = 'http: // biancheng.dnbc?info/pingjia.php? Id = 348544 'Rolling = 'no'>
There are three steps to write or read data into a file:
1. open a file (if any)
2. write/read files
3. close this file
L open a file
Before opening a file, we need to know the path of the file and whether the file exists.
Use the built-in global variables of $ _ SERVER ["DOCUMENT_ROOT"] to obtain the relative path of the site. As follows:
$ Root = $ _ SERVER ["DOCUMENT_ROOT"];
Use the file_exists () function to check whether a file exists. As follows:
If (! File_exists ("$ root/order.txt") {echo 'file does not exist ';}
Next, use the fopen () function to open the file.
$ Fp = fopen ("$ root/order.txt", 'AB ');
The fopen () function accepts two or three or four parameters.
The first parameter is the file path, and the second parameter is the operation method (read, write, append, and so on). required parameter.
$ Fp = fopen ("$ root/order.txt", 'AB ');
The third is an optional parameter. if you need PHP to search for a file in include_path, you can use it without providing a directory name or path.
$ Fp = fopen ("order.txt", 'AB', true );
The fourth parameter is also an optional parameter. it allows the file name to start with the protocol name (such as http: //) and open the file in a remote location. It also supports some other protocols, for example, ftp.
If fopen () successfully opens a file, a pointer to the file is returned. We saved it to the $ fp variable above.
File mode diagram
L write files
Writing files in PHP is relatively simple. Use the fwrite () function directly.
The fwrite () prototype is as follows:
Int fwrite (resource handle, string [, int length]);
The third parameter is optional, indicating the maximum length of the written file.
You can use the built-in strlen () function to obtain the string length, as follows:
Fwrite ($ fp, $ outputinfo, strlen ($ outputinfo ));
This function tells PHP to save the information in $ outputinfo to the file pointed to by $ fp.
L read files
1. open the file in read-only mode
The fopen () function is still used, but the file is opened in read-only mode, and the file mode is "rb. As follows:
$ Fp = fopen ("$ root/order.txt", 'RB ');
2. know when to read the file
We use the while loop to read the file content and the feof () function as the termination condition of the loop condition. As follows:
While (! Feof ($ fp )){
// Information to be processed
}
3. read a row of records each time.
The fgets () function can read a row of content from a text file. As follows:
$ Fp = fopen ("$ root/order.txt", 'RB ');
While (! Feof ($ fp )){
$ Info = fgets ($ fp, 999 );
Echo $ info .'
';
}
Fclose ($ fp );
In this way, he will continue to read data until he reads a line break (\ n), the file terminator EOF, or reads 998B from the file, the maximum length that can be read is the specified length minus 1B.
4. Read the entire file
PHP provides 4 different methods to read the entire file.
A). readfile () function
It can be used directly without fopen ($ path) files or close files or echo. As follows:
Readfile ("$ root/order.txt ");
It automatically outputs the file information to the browser. Its prototype is as follows:
Int readfile (string filename, [int use_include_path [, resource context]);
The second optional parameter specifies whether PHP searches for files in include_path. this is the same as that of the fopen function. the returned value is the total number of bytes read from the file.
Note: Use directly without fopen or fclose
B). fpassthru () function
To use this function, you must first open a file using fopen. Then, the file pointer is passed as a parameter to fpassthru (), so that the file content pointed to by the file pointer can be output. Then close the file. As follows:
$ Fp = fopen ("$ root/order.txt", 'RB ');
Fpassthru ($ fp );
Fclose ($ fp );
The returned value is also the total number of bytes read from the file.
Note: Both fopen and fclose are required.
C). file () function
In addition to outputting a file to a browser, the readfile () function is the same and sends the result to an array. As follows:
$ FileArray = file ("$ root/order.txt ");
Each row in the file is used as each element of the array.
Note: Use directly without fopen or fclose
D). file_get_contents () function
The same as readfile (), but this function will return the file content in the form of a string, instead of directly outputting the file content to the browser, that is, the echo output must be used, as shown below:
Echo file_get_contents ("$ root/order.txt ");
Note: Use directly without fopen or fclose
5. read one character
The fgetc () function reads a character from a file at a time. it has a file pointer function, which is a unique parameter and returns the next character. As follows:
$ Fp = fopen ("$ root/order.txt", 'RB ');
While (! Feof ($ fp )){
$ Char = fgetc ($ fp );
If (! Feof ($ fp )){
Echo ($ char = "\ n "? '
': $ Char );
}
}
Fclose ($ fp );
Note: One disadvantage of the fgetc () function is that it returns the object Terminator EOF, while fgets () does not. After reading characters, you also need to judge feof ().
6. read arbitrary length
The fread () function reads any length of bytes from a file. the Function prototype is as follows:
String fread (resource fp, int length );
When this function is used, it either fully reads the number of bytes specified by the length parameter, or reads the end of the file.
$ Fp = fopen ("$ root/order.txt", 'RB ');
Echo fread ($ fp, 10); // read 10 bytes
Fclose ($ fp );
L close the file
It is easy to close the file. you can call the fclose () function directly. if true is returned, the operation is successful. Otherwise, the operation is successful. As follows:
Fclose ($ fp );
L delete an object
Unlink () function (no function named delete), as follows:
Unlink ("$ root/order.txt ");
L determine the file size
You can use the filesize () function to view the size of a file (in bytes), as follows:
Echo filesize ("$ root/order.txt ");
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/348544.html pageNo: 3