File reading method for PHP file read and write operations

  • 2020-03-31 21:26:23
  • OfStack

PHP file read operations involve more PHP file manipulation functions than file write operations, which are described in more detail in code instances.
The way to read the data stored in the text file mainly involves three steps and partial file operation functions as follows:
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 is still an example of PHP file read and write operation code to explain the specific application of the file read method, in the example, by calling different PHP file read operation function to read the data in the text file, you can deepen the understanding of PHP file read operation function, so as to make a reasonable application in PHP website development. The data written in the text file is from the file writing tutorial for PHP file read and write operations. You can also refer to this article for the file read and write mode in the fopen function.
The PHP file reads an instance of the operation code
 
<? 
$readFun = "fread"; 
switch ($readFun) 
{ 
case "fgetss": 
@$fp = fopen("leapsoulcn.txt","r") or die("system error"); 
$allowable_tags = "<h1>"; 
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"?"<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 example above, you can call different PHP file reading methods by assigning a value to $readFun. The PHP file reading functions involved are fgets, fgetss, fgetcsv, readfile, fpassthru, file, fgetc, and so on.
The difference between the PHP file read operation functions fgets, fgetss, and fgetcsv
In the code example, the default PHP file read operation function is fgets, and the fgetss and fgetcsv functions do the same thing as fgets, reading one line of the file at a time until the end of the file. Here I set the length of the data in the read text file to 100, i.e., the maximum read length to 99 (100-1), so that when the newline \n or file terminator EOF or 99 bytes from the file are read, the data is stopped. The fgets function returns the data read by the file, string type.
The fgetss function is a variant of the fgets function. It can strip away PHP and HTML tags and filter unnecessary data by passing the third parameter, which can improve the security of the website.
 
string fgetss(resource fp,int length, string[optional] allowable_tags) 

The allowable_tags parameter is optional. In the example, I first wrote a line of text containing HTML, body, and h1 tags in the leapsoulcn.txt file, and then I set in the code that only h1 tags are allowed.
The fgetcsv function is another variant of fgets, except that when you write data in a text file using a delimiter, you can use fgetcsv to split a line into multiple lines and store the results in an array. The function prototype is shown below
 
array fgetcsv(resource fp,int length, string[optional] delimiter,string[optional] enclosure) 

Delimiter is optional, and since I used \t in the data I wrote to the file before, I used \t in the instance when the file read the delimiter in the function fgetcsv and printed out the array structure returned by fgetcsv with print_r.
Fgets, fgetss, and fgetcsv have the same thing in common: they all need to use fopen function to open the read file in advance, and feof function to determine whether the file pointer reaches the end of the file. Remember to use fclose function to close the file after the read operation is completed.
Fgetc: reads a single character
Fgetc function is used to read a character, in the code instance I read a character by a character, when encountered \n character will be converted to the br tags 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, is not recommended to use.
The difference between the PHP file read operation functions readfile, fpassthru, and file
What all three functions have in common is that they can read the entire file at once, rather than one line or character at a time. The difference is:
The readfile function opens the file and returns the contents of the file directly to the browser. Like the fopen function, the function returns the total number of characters in the file. The second argument to the readfile function is optional, indicating whether PHP should look for the file in the include_path. In the code instance, I used the echo statement not to output the contents of the file I read, but to output the total number of file characters I read. 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 by sending the contents of the file to an array, one array cell 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 the remaining data at the file pointer, that is, if the file pointer is not at the beginning, it outputs only the data after the file pointer. This function reads the given file pointer from its current position to EOF and writes the result to the output buffer, returning the number of characters output. Returns FALSE when an error occurs. In contrast to the readfile() function, the fpassthru() function opens the file first and closes it when the data is read.
Fread exists with file_exists, filesize
The fread function is also a way to read a file. It can read any byte from the file, either at length or to the end of the file. The prototype of the read function is as follows:
 string fread(resource fp,int length) 

When using 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, i.e
 
<? 
  @$fp = fopen("leapsoulcn.txt","r") or die("system error"); 
  echo fread($fp,filesize("leapsoulcn.txt")); 
  fclose($fp); 
?> 

We have not yet used the file_exists function in the PHP file read-write tutorial. Generally in PHP website development, for various reasons, sometimes when the file does not exist, we do not want to create a new file, so we need to use the file_exists function to determine whether the file exists before using the fopen function, i.e
 
<? 
if(file_exists("leapsoulcn.txt")) 
{ 
  //Read and write to PHP files
} 
?> 

Above is the PHP file read and write operation tutorial of the various methods of the file read and write operation, through the rational application of PHP file read and write operation function, you can achieve a simple message book, website log record and other functions.

Related articles: