Home >Backend Development >PHP Tutorial >Application example of PHP file reading function_PHP tutorial
This article mainly introduces application examples of PHP file reading function and how to use commonly used file data reading functions. The difference is very detailed, friends in need can refer to it.
PHP file reading operations involve more PHP file operation functions than file writing operations. These functions will be introduced in detail in the code examples.
The method of reading data stored in text files mainly involves three steps and some file operation functions as follows:
1. Open a 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 still uses an example of PHP file reading and writing operation code to explain the specific application of the file reading method. In the example, by calling different PHP file reading operation functions to read the data in the text file, you can deepen PHP file reading. Get an understanding of operation functions so that they can be reasonably applied in PHP website development. The data written in the text file comes from the file writing tutorial of PHP file reading and writing operations. You can also refer to this article for the file reading and writing mode in the fopen function.
PHP file reading operation code example
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
$readFun = "fread"; switch ($readFun) { case "fgetss": @$fp = fopen("leapsoulcn.txt","r") or die("system error"); $allowable_tags = " ";while (!feof($fp)) { $output = 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"?" } 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 call different PHP file reading methods by assigning a value to $readFun. The PHP file reading operation functions involved are fgets, fgetss, fgetcsv, readfile, fpassthru, file, fgetc functions.
The difference between PHP file reading operation functions fgets, fgetss, and fgetcsv
In the code example, the default PHP file reading operation function is fgets. The functions of fgetss and fgetcsv functions are the same as fgets, and they both read the file one line at a time until the end of the file. Here I set the data length to be read in the text file to 100, that is, the maximum read length is 99 (100-1). In this way, when the newline character n or the end of file character EOF is encountered, or 99 is read from the file Stop reading data when bytes are reached. The fgets function returns the data read from the file, in string type.
The fgetss function is a variant of the fgets function. It can strip PHP and HTML tags and filter unnecessary data by passing the third parameter, which can improve website security. For example, user input data can be filtered in the guestbook. The fgetss function prototype is as follows:
string fgetss(resource fp,int length, string[optional] allowable_tags)
The allowable_tags parameter is optional. In the example, I wrote a line of text containing html, body, and h1 tags in the leapsoulcn.txt file in advance, and then in the code I set that only the h1 tag can appear.
The fgetcsv function is another variant of fgets. The difference is that when the data written in your text file uses delimiters, you can use fgetcsv to break one line into multiple lines, and the returned result is stored in an array. , the function prototype is as follows:
array fgetcsv(resource fp,int length, string[optional] delimiter,string[optional] enclosure)
Delimiter is optional. Since I used t in the data written to the file before, in the example, I used t as the delimiter in the file reading function fgetcsv, and then printed out the value returned by fgetcsv through print_r. Array structure.
The three PHP file reading operation functions fgets, fgetss, and fgetcsv have in common that they all need to use the fopen function to open the read file in advance, and at the same time use the feof function to determine whether the file pointer reaches the end of the file. Remember to complete the reading operation Then use the fclose function to close the file.
fgetc: Read a single character
The fgetc function is used to read a character. In the code example, I read the characters one by one. When n characters are encountered, they are converted into br tags in the html file so that specific line breaks can be displayed in the browser. Of course, the efficiency of this function is definitely relatively low, and its use is not recommended.
The difference between PHP file reading operation functions readfile, fpassthru, and file
What the three functions have in common is that they can read the entire file at one time, instead of reading one line or one character at a time. The difference is:
The readfile function opens the file, returns the file content and outputs it directly on the browser. Like the fopen function, the function return value is the total number of characters in the file. The second parameter of the readfile function is optional, indicating whether PHP should be in the include_path. Find files. In the code example, I use the echo statement not to output the read file content, but to output the total number of file characters read. The read file content has been automatically output by the readfile function. This must be clear! The readfile function prototype is as follows:
int readfile(string filename,int[optional] use_include_path)
The file function is another way to read a file. It sends the read file content to an array, with one array unit per line. 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, 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 return value is the number of characters output. When an error occurs, FALSE is returned. Compared with the readfile() function, the fpassthru() function needs to open the file first, and then close the file after the data is read.
fread and file_exists, filesize function
The fread function is also a method of reading files. It can read any bytes from the file, either to meet the length or to the end of the file. The read function prototype is as follows:
String fread(resource fp,int length)
When using the fread function, when you want to read all the data in the file but do not know the file data length, the filesize function can solve this problem, that is,
?
|
<🎜> <🎜> @$fp = fopen("leapsoulcn.txt","r") or die("system error");<🎜> <🎜> echo fread($fp,filesize("leapsoulcn.txt"));<🎜> <🎜> fclose($fp);<🎜> <🎜>?> |
In the PHP file reading and writing operation tutorial, we have not used the file_exists function. Usually in PHP website development, due to various considerations, sometimes when the file does not exist, we do not like to create a new file. At this time we You need to use the file_exists function to determine whether the file exists before using the fopen function, that is,
?
2 3 4 5 6 |
if(file_exists("leapsoulcn.txt")) |