PHP file read operations involve more PHP file operation functions than file write operations. These functions are described in detail in the Code instance.
The Data Storage Method for reading text files involves the following three steps and some file operation functions:
1. open the file (file operation function: fopen)
2. file Data Reading (file operation functions: fgets, file, readfile, feof, etc)
3. close the file (file operation function: fclose)
The following uses the PHP file read/write operation code instance to explain the specific application of the file read method. In the instance, you can call different PHP file read operation functions to read data in the text file, you can learn more about the function for reading PHP files so that they can be used properly in PHP website development. The data written to a text file comes from the PHP file read/write operation file writing tutorial. For details about the file read/write mode in the fopen function, refer to this article.
PHP file read operation code instance
Copy codeThe Code is as follows:
<?
$ ReadFun = "fread ";
Switch ($ readFun)
{
Case "fgetss ":
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
$ Allowable_tags = "While (! Feof ($ fp )){
$ Outputs = fgetss ($ fp, 100, $ allowable_tags );
Echo $ output;
}
Fclose ($ fp );
Break;
Case "fgetcsv ":
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
While (! Feof ($ fp )){
$ Output = fgetcsv ($ fp, 100, "\ t ");
Print_r ($ output );
}
Fclose ($ fp );
Break;
Case "readfile ":
Echo readfile ("leapsoulcn.txt ");
Break;
Case "fpassthru ":
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
If (! Fpassthru ($ fp ))
Exit ();
Fclose ($ fp );
Break;
Case "file ":
$ Output = file ("leapsoulcn.txt ");
Print_r ($ output );
Break;
Case "fgetc ":
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
While (! Feof ($ fp )){
$ Str = fgetc ($ fp );
Echo ($ str = "\ n "? "<Br/>": $ str );
}
Fclose ($ fp );
Break;
Case "fread ":
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
Echo fread ($ fp, 300 );
Fclose ($ fp );
Break;
Default:
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
While (! Feof ($ fp )){
$ Output = fgets ($ fp, 100 );
Echo $ output;
}
Fclose ($ fp );
Break;
}
?>
Note: In the above example, you can assign a value to $ readFun to call different methods for reading PHP files, the involved PHP file read operation functions include fgets, fgetss, fgetcsv, readfile, fpassthru, file, and fgetc.
Differences between functions fgets, fgetss, and fgetcsv for PHP File Reading
In code instances, the default PHP file read operation function is fgets. The fgetss and fgetcsv functions are the same as fgets functions. They read one row of the file at a time until the end of the file. Here, I set the Data Length in the read text file to 100, that is, the maximum read length is 99 (100-1, when the linefeed \ n or the file Terminator EOF or 99 bytes are read from the file, Data Reading is stopped. The fgets function returns the data read by the file, which is a string type.
The fgetss function is a variant of the fgets function. It can strip PHP and HTML tags and pass a third parameter to filter unnecessary data, which improves website security, for example, the message book can filter user input data. The fgetss function prototype is as follows:
Copy codeThe Code is as follows:
String fgetss (resource fp, int length, string [optional] allowable_tags)
Allowable_tags.txt is optional. In this example, a line of text containing the html, body, and h1 mark is written in the leapsoulcn.txt file. Then, in the code, I set that only the h1 mark can appear.
The fgetcsv function is another variant of fgets. The difference is that when the data written to your text file uses a delimiter, you can use fgetcsv to split a row into multiple rows, the returned results are stored in the array. The function prototype is as follows:
Copy codeThe Code 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 written to the file, I used \ t in the fgetcsv function for file reading in the instance, then print out the array structure returned by fgetcsv through print_r.
The three PHP file read operation functions fgets, fgetss, and fgetcsv all need to use the fopen function to open the file to be read in advance, and use the feof function to determine whether the file pointer has reached the end of the file, remember to use the fclose function to close the file after the read operation is complete.
Fgetc: read a single character
The fgetc function is used to read a character. In the code example, I read the character one by one. When the \ n character is encountered, it is converted into the br mark in the html file, in order to display the specific line feed effect in the browser, of course, the efficiency of this function is certainly relatively low, not recommended.
Differences between the readfile, fpassthru, and file functions for PHP file Reading
In common, the three functions can read the entire file at a time, rather than one row or character at a time. The difference is:
The readfile function opens the file and returns the file content directly to the browser. Like the fopen function, the function returns the total number of characters in the file. The second parameter of the readfile function is optional, specifies whether PHP should search for files in include_path. In code instances, the echo statement is not used to output the Read File Content, but to output the total number of characters read from the file. The Read File Content readfile function has been automatically output, which must be clear! The readfile function is prototype as follows:
Copy codeThe Code is as follows:
Int readfile (string filename, int [optional] use_include_path)
The file function is another method for reading files. It sends the content of the read files to an array with one array unit per row. The file function is prototype as follows:
Copy codeThe Code is as follows: array file (string filename, bool [optional] use_include_path)
The fpassthru () function is used to output all the remaining data at the file pointer. That is, if the file pointer is not at the beginning, it only outputs the data after the file pointer. This function reads the given file pointer from the current position to EOF and writes the result to the output buffer. The returned value is the number of output characters. If an error occurs, FALSE is returned. Compared with the readfile () function, the fpassthru () function must first open the file and close the file after the data is read.
Fread, file_exists, and filesize Functions
Fread function is also a method for reading files. It can read any byte from the file, either the length or the end of the file. The read function prototype is as follows:
Copy codeThe Code is as follows: string fread (resource fp, int length)
When you use the fread function, The filesize function can solve this problem when you want to read all the data in the file and do not know the length of the file data. That is
Copy codeThe Code is as follows:
<?
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
Echo fread ($ fp, filesize ("leapsoulcn.txt "));
Fclose ($ fp );
?>
In the PHP file read/write tutorial, we have not used the file_exists function. Generally, for various considerations, sometimes when the file does not exist, we do not like creating a new file, in this case, we need to use the file_exists function before using the fopen function to determine whether the file exists, that is
Copy codeThe Code is as follows:
<?
If (file_exists ("leapsoulcn.txt "))
{
// Read and write PHP files
}
?>
The above is an introduction to various methods of File Read operations in the PHP File Read/write operation tutorial. By properly applying the PHP file read/write operation function, you can implement simple message books, website log records, and other functions.