PHP uses fgets() function to read file content examples in detail

怪我咯
Release: 2023-03-13 13:34:01
Original
2272 people have browsed it

php fgets() Function is used to read a line from the file pointer. This function can be combined with the feof function to read the file content. This article Let me share with you a simple example (demo) of using fgets() in PHP to read/obtain file content. Friends in need can refer to it.

First introduce the syntax of php fgets() function

fgets(file,length)
Copy after login

Detailed explanation of parameters

file Required. Specifies the file to be read.

length Optional. Specifies the number of bytes to read. The default is 1024 bytes.

Description

Read a line from the file pointed to by file and return a string with a length of at most length - 1 byte. Stops when a newline character (included in the return value), EOF, or length - 1 bytes has been read (whichever occurs first). If length is not specified, it defaults to 1K, or 1024 bytes.

If it fails, return false.

The following introduces the principle of PHP using the fgets() function to read files.

The fgets() function is used to read a line in the file. Therefore, we can always call the fgets() function to read the entire content of the file, and we can use the feof function to determine whether the last line of the file has been reached. , if the last line is reached, stop calling the fgets() function.

The specific example of the fgets() function reading the file is as follows:

Suppose we have a file data.txt with the following content:

a@example.com|Alice 
b@example.org|Bill 
c@example.com|Charlie 
c@example.com|China 
c@example.com|Phpcn
Copy after login

We use fgets() The function reads the contents of the file. The code is as follows:

<?php
$fh = fopen(&#39;data.txt&#39;,&#39;rb&#39;);
if (! $fh) {
    print "Error opening people.txt: $php_errormsg";
} else {
    for ($line = fgets($fh); ! feof($fh); $line = fgets($fh)) {
        if ($line === false) {
            print "Error reading line: $php_errormsg";
        } else {
			echo $line."<br/>";
        }
    }
    if (! fclose($fh)) {
        print "Error closing people.txt: $php_errormsg";
    }
}
?>
Copy after login

First use the fopen function to open the file. The fopen function will return a file pointer, which will be used by the fgets function. Then use the fgets function to read a line in the file. Before reading, you must determine whether the last line has been reached. If the last line is reached, stop reading.

The above is the detailed content of PHP uses fgets() function to read file content examples in detail. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!