This article mainly introduced the PHP file read the application example and the commonly used file data reads the function the use method and the difference, very detailed, has the need the small partner to be possible to refer under.
PHP file read operations involve more PHP file manipulation functions than file writes, which are described in detail in code instances.
The way in which data is stored in a text file is mainly three steps and some file operation functions are as follows:
1, open the file (file operation function: fopen)
2, file data read (File operation function: fgets, file, ReadFile, feof, etc.)
3, close the file (file operation function: fclose)
The following is still a PHP file read and write operation code examples to explain the specific application of file reading methods, in the example, by invoking the different PHP file read operation function read the text file data, you can deepen the PHP file read operation function of understanding, in order to PHP Web site development Reasonable application. The data written in the text file comes from the file writing tutorial of PHP file reading and writing, which can also be referred to in the fopen function in relation to the file reading and writing mode.
PHP file read Operation code instance
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
|
Note: In the above example, you can use the $readfun assignment to achieve different PHP file reading method calls, involved in the PHP file read operation functions are fgets, FGETSS, Fgetcsv, ReadFile, fpassthru, file, Fgetc and other functions.
PHP file read operation function fgets, FGETSS, fgetcsv the difference between
In the code instance, the default PHP file read action function is the function of FGETS,FGETSS and fgetcsv function is the same as fgets, is to read a row in the file, until the end of the file. Here I set the length of the read text file to be 100, that is, the maximum read length is 99 (100-1), so that the data is stopped when a newline character n or a file terminator EOF is encountered or 99 bytes are read from the file. The Fgets function returns the data read by the file, the string type.
The FGETSS function is a variant of the fgets function that can strip PHP and HTML tags and filter unnecessary data by passing the third parameter, which can improve the security of the site, such as filtering user input data in the message book, and FGETSS function prototypes as follows:
String Fgetss (Resource fp,int length, string[optional] allowable_tags)
The allowable_tags parameter is optional, in which I have written a line of HTML, body, and H1 tags in the leapsoulcn.txt file in advance, and then in the code I set only allow the H1 tag to appear.
The Fgetcsv function is another variant of fgets, the difference being that, when the data written in your text file uses delimiters, you can use Fgetcsv to break a row into multiple rows, and the returned results are stored in an array, and the function prototype is as follows:
Array Fgetcsv (Resource fp,int length, string[optional] delimiter,string[optional] enclosure)
Delimiter is optional, because I used t in the data previously written to the file, so in the instance the delimiter in the file read function Fgetcsv I used t, and then through Print_r I printed out the array structure returned by Fgetcsv.
Three PHP file read operation functions fgets, FGETSS, fgetcsv in common is the need to use the fopen function in advance to open the read file, while the feof function to determine whether the file pointer to the end of the file, remember to use the Fclose function after the completion of the read operation to close the file.
FGETC: Reading a single character
The FGETC function is used to read a character, in the code instance I pass a character reading, when I encounter n characters, I convert it to the BR mark in the HTML file so that I can show the concrete wrapping effect in the viewer, the efficiency of this function is certainly low, it is not recommended to use.
The difference between PHP file read operation function ReadFile, fpassthru, file
Three functions in common is that you can read the entire file at once, not one line or one character at a time. The difference is:
The ReadFile function opens the file and returns the contents of the file directly to the viewer, as with the fopen function, the function return value is the total number of characters in the file, and the second parameter of the ReadFile function is optional, indicating whether PHP should find the file in Include_path. In the code instance, I use the Echo statement not to output the read file content, but to output the total number of file characters read, the contents of the file ReadFile function has been automatically output, this must be clear!readfile function prototype is as follows:
int ReadFile (string filename,int[optional] use_include_path)
The file function is another way to read files, which is to send the contents of the read file to an array of one array unit per row. The file function prototype is as follows:
Array file (string filename,bool[optional] use_include_path)
The Fpassthru () function is used to output all remaining data at the file pointer, which outputs only the data after the file pointer if the file pointer is not at the beginning. The function reads the given file pointer from the current position to EOF and writes the result to the output buffer, with the returned value of the number of characters output. Returns False when an error occurs. Compared to the ReadFile () function, the Fpassthru () function needs to open the file first, and then close the file when the data is read.
Fread and file_exists, filesize functions
The Fread function is also a way to read a file that reads any byte from the file, either satisfying length or reading to the end of the file. The Read function prototype is as follows:
String Fread (Resource fp,int length)
When you use the Fread function, the FileSize function solves the problem when you want to read all the data in the file without knowing the length of the file data.
?
1 2 3 4 5 |
? @ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error"); Echo fread ($fp, FileSize ("Leapsoulcn.txt")); Fclose ($FP);?> |
We have not used the file_exists function in the PHP file reading and writing course, usually in the PHP website development, for various considerations, sometimes when the file does not exist, we do not like to create the new file, then we need to use the fopen function before using the File_ The EXISTS function determines whether a file exists, i.e.
?
1 2 3 4 5 6 |
? if (file_exists ("Leapsoulcn.txt")) {//php file read and write operation}?> |
The above for everyone to read the contents of the PHP file of various methods, through the rational application of PHP file read and write operation function, you can achieve a simple message book, Web log and other functions.
Note < > : More Wonderful tutorials please focus on Triple programming