EOF is a very important concept, and almost every major programming language provides built-in functions to verify that the parser has reached the file EOF. In PHP, this function is feof (). The feof () function is used to determine whether the end of the resource is reached. It is often used in file I/O operations. The form is:
int feof (string resource)
The instance code is as follows:
$fh = fopen ("/home/www/data/users.txt", "RT");
while (!feof ($FH)) echo fgets ($FH);
Fclose ($FH);
?>
BOOL Feof (Resource $handle): Tests for End-of-file on a file pointer
This PHP manual the exact words above.
For convenience, I used to use it this way.
If file can not be read or doesnt exist fopen function returns FALSE
$file = @fopen ("No_such_file", "R");
FALSE from fopen'll issue warning and result in infinite loop here
while (!feof ($file)) {
}
Fclose ($file);
?>
Indeed, this is easier to use. However, if the above variable $file is not a valid file pointer or has been closed by fclose.
Then in the sixth line of the program, there will be a waring, concurrent life cycle.
Why?
The reason is
Returns TRUE If the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.
So, for security reasons, it is better to use the above code to add a judgment, is_resource or relatively safe
http://www.bkjia.com/PHPjc/486144.html www.bkjia.com true http://www.bkjia.com/PHPjc/486144.html techarticle EOF is a very important concept, and almost every major programming language provides built-in functions to verify that the parser has reached the file EOF. In PHP, this function is feof (). Feo ...