PHP file reading operation involves more PHP file operation functions than file writing operation, which will be detailed in the code example Introduce these functions.
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
<? $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 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.
What the three PHP file reading operation functions fgets, fgetss, and fgetcsv have in common is 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 wait until the reading operation is completed. 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 the specific line break effect 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 a file and returns the file content to be output 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 and indicates whether PHP should search for the file 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:
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与file_exists、filesize函数
fread函数也是读取文件的一种方法,其可以从文件中读取任意字节,要么满足length要么读到文件末尾。read函数原型如下:
string fread(resource fp,int length)
在用到fread函数时,当你想读取文件全部数据,又不知道文件数据长度时,filesize函数可以解决这个问题,即
<? @$fp = fopen("leapsoulcn.txt","r") or die("system error"); echo fread($fp,filesize("leapsoulcn.txt")); fclose($fp); ?>
在PHP文件读写操作教程中我们还没有使用过file_exists函数,通常在PHP网站开发中,出于各种考虑,有时当文件不存在时,我们并不像创建新文件,这时我们就需要在使用fopen函数前使用file_exists函数判断文件是否存在,即
<? if(file_exists("leapsoulcn.txt")) { //进行PHP文件读写操作 } ?>
以上为大家介绍了PHP读取文件内容的各种方法,通过合理应用PHP文件读写操作函数,可以实现简单的留言本、网站log记录等功能。