This example describes how PHP uses the feof () function to read files. Share to everyone for your reference. The specific usage is as follows:
Feof applies to PHP 4, PHP 5
-Used to test whether the file pointer is at the end of the file.
If the server does not close the connection that was opened by Fsockopen (), feof () will wait until the timeout returns true. The default time-out limit is 60 seconds, and you can use Stream_set_timeout () to change this value.
The file pointer must be valid and must point to a file that was successfully opened by fopen () or fsockopen () and not yet closed by fclose ().
If an invalid file pointer is passed, it may fall into an infinite loop because EOF does not return TRUE.
Example #1 feof () example using an invalid file pointer:
Copy Code code as follows:
<?php
If the file is unreadable or does not exist, the fopen function returns FALSE
$file = @fopen ("No_such_file", "R");
FALSE from fopen emits a warning message and gets stuck in an infinite loop here.
while (!feof ($file)) {
}
Fclose ($file);
?>
Example:
Copy Code code as follows:
<?php
$file = fopen ($_server[' document_root '). " /me/test.txt "," R ");
Prints all the lines in the text until the end of the file.
while (! feof ($file))
{
Echo fgets ($file). "<br/>";
}
Fclose ($file);
?>
Output:
Hello, this is a test file.
There are three lines here.
This is the last line.
I hope this article will help you with your PHP program design.