Home  >  Article  >  Backend Development  >  Detailed explanation of file reading method for PHP file reading and writing operations_PHP tutorial

Detailed explanation of file reading method for PHP file reading and writing operations_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:15853browse

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 way to read data stored in text files mainly involves three steps and some file operation functions as follows:
1. Open the file (file operation function: fopen)
2. Read file data (file Operation functions: fgets, file, readfile, feof, etc.)
3. Close the file (file operation function: fclose)

The following still uses PHP file reading and writing operation code examples 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 your understanding of the PHP file reading 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

Copy code The code is as follows:

$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"?"
":$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 call different PHP file reading methods by assigning a value to $readFun, involving PHP file reading operations. Functions include fgets, fgetss, fgetcsv, readfile, fpassthru, file, fgetc and other 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 are all read at once. Take a line from the file 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:
Copy code The code 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 only allow h1 tags 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
Copy code The code 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, and when n characters are encountered, they are converted into br tags in the html file. In order to display the specific line wrapping effect in the browser, of course the efficiency of this function is definitely relatively low and 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 Find files in include_path. 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:
Copy code The code 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:
Copy code The code 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 close the file after the data is read.
 fread, 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:
Copy code The code 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,
Copy code The code is as follows:

 @$fp = fopen("leapsoulcn.txt","r") or die("system error");
echo fread( $fp,filesize("leapsoulcn.txt"));
fclose($fp);
?>

We have not used it in the PHP file reading and writing tutorial 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, then we need to use the file_exists function to determine whether the file exists before using the fopen function, that is
Copy code The code is as follows:

if(file_exists("leapsoulcn.txt"))
{
 //Perform PHP file reading and writing operations
}
?>

The above are the various methods of file reading operations in the PHP file reading and writing operation tutorial Introduction, through the reasonable application of PHP file read and write operation functions, simple guestbook, website log recording and other functions can be realized.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322840.htmlTechArticlePHP file reading operation involves more PHP file operation functions than file writing operation, in the code example These functions will be described in detail. The main ways to read data stored in text files...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn