Home > Backend Development > PHP Tutorial > Application of PHP file operation functions

Application of PHP file operation functions

墨辰丷
Release: 2023-03-31 13:16:02
Original
1620 people have browsed it

This article mainly introduces the application examples of PHP file reading function and the usage and differences of commonly used file data reading functions. It 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 the file (file operation function: fopen)
2. File data reading (file operation function: fgets, file, readfile, feof, etc.)
3. Close the file (file operation function: fclose)

The following still uses PHP file read and write operation code examples to explain files. The specific application of the 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

<?
$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;
}
?>
Copy after login

Note: In the above example, you can call different PHP file reading methods by assigning a value to $readFun. The PHP file reading involved The fetch operation functions include fgets, fgetss, fgetcsv, readfile, fpassthru, file, fgetc and other functions.

The difference between the PHP file reading operation functions fgets, fgetss, and fgetcsv

In the code example, the default PHP file reading operation function is fgets, fgetss and The function of fgetcsv function is the same as that of fgets, which reads one line from the file at a time until the end of the file. Here I set the data length in the text file to be read 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 the reading from the file is completed, Stop reading data when 99 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. It can improve website security. For example, the guestbook can filter users' Input data, the fgetss function prototype is as follows:

string fgetss(resource fp,int length, string[optional] allowable_tags)

allowable_tags parameter is optional, in the example I 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 decompose one line into multiple lines, and the returned result is stored In the array, the function prototype is as follows:

array fgetcsv(resource fp,int length, string[optional] delimiter,string[optional] enclosure)

delimiter is available option, since I used \t in the data written to the file before, so in the example I used \t as the delimiter in the file reading function fgetcsv, and then printed out the array structure returned by fgetcsv through print_r.

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 read After the fetch operation is completed, 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 the \n character is encountered, it is converted into an html file. The br tag is used to display specific line wrapping effects 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, not at once Read a line or character. 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 prototype of the readfile function is as follows:
int readfile(string filename,int[optional] use_include_path)

The file function is another method of reading files. It sends the read file contents to an array. , one array cell per row. The file function prototype is as follows:
array file(string filename,bool[optional] use_include_path)

  fpassthru()函数用来输出文件指针处的所有剩余数据,即如果文件指针并不在开头,它只输出文件指针后面的数据。该函数将给定的文件指针从当前的位置读取到EOF,并把结果写到输出缓冲区,返回值为输出的字符数。发生错误时,返回FALSE。与readfile()函数相比,fpassthru()函数需要首先打开文件,数据读取完毕后要关闭文件。

  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);
?>
Copy after login

在PHP文件读写操作教程中我们还没有使用过file_exists函数,通常在PHP网站开发中,出于各种考虑,有时当文件不存在时,我们并不像创建新文件,这时我们就需要在使用fopen函数前使用file_exists函数判断文件是否存在,即

<?
if(file_exists("leapsoulcn.txt"))
{
  //进行PHP文件读写操作
}
?>
Copy after login

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP自动生成表单的方法

php操作MySQL数据库的方法

php针对服务器端预定义变量$_SERVER的方法

The above is the detailed content of Application of PHP file operation functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template