Php 简明教程
PHP - Read File
PHP 中有许多选项可用于读取已使用 fopen() 函数打开的文件中的数据。PHP 库中的以下内置函数可以帮助我们执行读取操作 −
There are a number of options in PHP for reading data from a file that has been opened with the fopen() function. The following built-in functions in PHP’s library can help us perform the read operation −
-
fgets() − gets a line from the file pointer.
-
fgetc() − returns a string with a single character from the file pointer.
-
fread() − reads a specified number of bytes from the file pointer.
-
fscanf() − reads data from the file and parses it as per the specified format.
The fgets() Function
fgets() 函数可以从一个打开的文件中返回一行。此函数停止在指定长度的新行或 EOF 处返回,以先遇到的为准,并在失败时返回 false 。
The fgets() function can return a line from an open file. This function stops returning on a new line at a specified length or EOF, whichever comes first and returns false on failure.
fgets(resource $stream, ?int $length = null): string|false
此处, $stream 参数是使用 fopen() 函数以读取或读/写模式打开的文件的文件指针或句柄,而 $length 是指定要读取的字节数的可选参数。
Here, the $stream parameter is a file pointer or handle to the file opened with the fopen() function with read or read/write mode, and $length is an optional parameter specifying the number of bytes to be read.
当读取到“length-1”个字节或遇到新行时,读取操作结束,以先遇到的为准。
The read operation ends when "length-1" bytes are read or a newline is encountered, whichever is first.
Example
以下代码从“hello.txt”文件中读取第一行可用行 −
The following code reads the first available line from the "hello.txt" file −
<?php
$file = fopen("hello.txt", "r");
$str = fgets($file);
echo $str;
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World
Example
你可以将 fgets() 函数放在一个循环中,以便在到达文件结尾之前读取文件。
You can put the fgets() function in a loop to read the file until the end of file is reached.
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
echo fgets($file). "<br>";
}
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World
TutorialsPoint
PHP Tutorials
此处,我们使用了 feof() 函数,如果文件指针处于 EOF,则返回 true;否则,返回 false。
Here, we have used the feof() function which returns true if the file pointer is at EOF; otherwise returns false.
The fgetc() Function
fgetc() 函数返回从文件句柄当前位置读取的单个字符。遇到 EOF 时,它返回 false 。
The fgetc() function returns a single character read from the current position of the file handle. It returns false when EOF is encountered.
fgetc(resource $stream): string|false
此处, $stream 参数是使用 fopen() 函数以读取或读/写模式打开的文件的文件指针或句柄。
Here, the $stream parameter is a file pointer or handle to the file opened with the fopen() function with read or read/write mode.
Example
以下代码显示从“hello.txt”文件中读取的第一个字符 −
The following code displays the first character read from the "hello.txt" file −
<?php
$file = fopen("hello.txt", "r");
$str = fgets($file);
echo $str;
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
H
Example
你还可以将 fgetc() 函数放在一个循环内,以便逐个字符地读取文件,直到到达 EOF。
You can also put the fgetc() function inside a loop to read the file character by character until it reaches EOF.
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
$char = fgetc($file);
if ($char == "\n")
echo "<br>";
echo $char;
}
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World
TutorialsPoint
PHP Tutorials
The fread() Function
PHP 中的 fread() 函数是用于从文件读取数据的二进制安全函数。虽然 fgets() 函数仅从文本文件中读取,但 fread() 函数可以以二进制模式读取文件。
The fread() function in PHP is a binary-safe function for reading data from a file. While the fgets() function reads only from a text file, the fread() function can read a file in binary mode.
fread(resource $stream, int $length): string|false
此处, $stream 参数是使用 fopen() 函数以二进制读取或读/写模式( rb 或 rb+ )打开的文件的文件指针或句柄。 $length 参数指定要读取的字节数。
Here, the $stream parameter is a file pointer or handle to the file opened with the fopen() function with binary read or read/write mode (rb or rb+). The $length parameter specifies number of bytes to be read.
如果未给出 $length 参数,PHP 将尝试读取整个文件,直到到达 EOF,但需符合指定的块大小。
If the $length parameter is not given, PHP tries to read the entire file until EOF is reached, subject to the chunk size specified.
Example
以下代码读取一个文本文件 −
The following code reads a text file −
<?php
$name = "hello.txt";
$file = fopen($name, "r");
$data = fread($file, filesize($name));
echo $data;
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World TutorialsPoint PHP Tutorials
Example
你还可以读取以 rb 模式打开的图像文件等非 ASCII 文件。
You can also read a non-ASCII file such as an image file opened in rb mode.
<?php
$name = "welcome.png";
$file = fopen($name, "rb");
$data = fread($file, filesize($name));
var_dump($data);
fclose($file);
?>
浏览器将“var_dump”信息显示为以下内容 −
The browser displays the "var_dump" information as the following −
The fscanf() Function
PHP 中的 fscanf() 函数从文件流中读取输入并根据特定格式对其进行解析,从而将其转换为指定类型的变量。每次调用此函数都将从文件中读取一行。
The fscanf() function in PHP reads the input from a file stream and parses it according to the specified format, thereby converts it into the variables of respective types. Each call to the function reads one line from the file.
fscanf(resource $stream, string $format, mixed &...$vars): array|int|false|null
此处, $stream 参数是指针,它指向使用 fopen() 函数打开的文件,且为读取模式。同时, $format 为一个字符串,包含下列一个或多个格式说明符 −
Here, the $stream parameter is the handle to the file opened with the fopen() function and in read mode. And, $format is a string containing one or more of the following formatting specifiers −
-
%% − Returns a percent
-
%b − Binary number
-
%c − The character according to the ASCII value
-
%f − Floating-point number
-
%F − Floating-point number
-
%o − Octal number
-
%s − String
-
%d − Signed decimal number
-
%e − Scientific notation
-
%u − Unsigned decimal number
-
%x − Hexadecimal number for lowercase letters
-
%X − Hexadecimal number for uppercase letters
$vars 是一个可选参数,它通过引用指定包含解析值的变量。
$vars is an optional parameter that specifies variables by reference which will contain the parsed values.
假设“employees.txt”文件在与下方 PHP 脚本相同的目录中。文本文件中每一行都有每个员工的 name, email, post 和 salary ,由制表符分隔。
Assuming that the "employees.txt" file is available in the same directory in which the PHP script given below is present. Each line in the text file has name, email, post and salary of each employee, separated by tab character.
Example
以下 PHP 脚本使用 fscanf() 函数中的格式说明符读取文件 −
The following PHP script reads the file using the format specifiers in fscanf() function −
<?php
$fp = fopen("employees.txt", "r");
while ($employee_info = fscanf($fp, "%s\t%s\t%s\t%d\n")) {
list ($name, $email, $post, $salary) = $employee_info;
echo "<b>Name</b>: $name <b>Email</b>:
$email <b>Salary</b>: Rs. $salary <br>";
}
fclose($fp);
?>
它将生成以下 output −
It will produce the following output −
Name: Ravishankar Email: ravi@gmail.com Salary: Rs. 40000
Name: Kavita Email: kavita@hotmail.com Salary: Rs. 25000
Name: Nandkumar Email: nandu@example.com Salary: Rs. 30000