This article mainly introduces how to use the feof () function to read files in PHP, compares the correct and incorrect usage in the form of examples, and clarifies the usage skills of the feof () function, for more information, see
This article mainly introduces how to use the feof () function to read files in PHP, compares the correct and incorrect usage in the form of examples, and clarifies the usage skills of the feof () function, for more information, see
This example describes how to use the feof () function to read files in PHP. Share it with you for your reference. The usage is as follows:
Feof applies to PHP 4 and PHP 5
-It is used to test whether the file pointer has reached the end of the file.
If the server does not close the connection opened by fsockopen (), feof () waits until it times out and returns TRUE. The default timeout limit is 60 seconds. You can use stream_set_timeout () to change this value.
The file pointer must be valid and must be directed to a file successfully opened by fopen () or fsockopen () (not closed by fclose ).
If the passed file pointer is invalid, it may be in an infinite loop, because EOF does not return TRUE.
Example #1 Example of using feof () with invalid file pointer:
The Code is as follows:
<? Php
// If the file cannot be read or does not exist, the fopen function returns FALSE.
$ File = @ fopen ("no_such_file", "r ");
// FALSE from fopen generates a warning message and falls into an infinite loop here
While (! Feof ($ file )){
}
Fclose ($ file );
?>
Example:
The Code is as follows:
<? Php
$ File = fopen ($ _ SERVER ['document _ root']. "/me/test.txt", "r ");
// Output all rows 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 help you with PHP programming.