How to read files using feof() function in PHP, _PHP tutorial

WBOY
Release: 2016-07-13 10:14:44
Original
1147 people have browsed it

PHP uses the feof() function to read files,

The example in this article describes how PHP uses the feof() function to read files. Share it with everyone for your reference. The specific usage is as follows:

feof applies to PHP 4, PHP 5
- Used to test whether the file pointer reaches the end of the file.

If the server does not close the connection opened by fsockopen(), feof() will wait until timeout and return TRUE. The default timeout limit is 60 seconds, this value can be changed using stream_set_timeout().

The file pointer must be valid and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

You may end up in an infinite loop if you pass an invalid file pointer because EOF does not return TRUE.
Example #1 feof() example using invalid file pointer:

Copy code The code is as follows:
// If the file cannot be read or does not exist, the fopen function returns FALSE
$file = @fopen("no_such_file", "r");

// FALSE from fopen will issue a warning message and get stuck in an infinite loop here
while (!feof($file)) {
}
fclose($file);
?>



Example:

Copy code The code is as follows:
$file = fopen($_SERVER['DOCUMENT_ROOT']."/me/test.txt", "r");

//Output all lines in the text until the end of the file.
while(! feof($file))
{
echo fgets($file). "
";
}
fclose($file);
?>

Output:
Hello, this is a test file.
There are three lines here.
This is the last line.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907842.htmlTechArticlePHP uses the feof() function to read files. This article describes how PHP uses the feof() function to read files. method. Share it with everyone for your reference. The specific usage is as follows: feof is applied to PHP 4,...
Related labels:
php
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