PHP feof() Function
Definition and Usage
The feof() function checks if the end of the file (eof) has been reached.
Returns TRUE if the file pointer reaches EOF or an error occurs, otherwise returns an error (including socket timeout), and returns FALSE otherwise.
Syntax
feof(file)
Parameter | Description |
---|---|
file | Required. Specifies the open file to be checked. |
Description
file The parameter is a file pointer. This file pointer must be valid and must point to a file opened by fopen() or fsockopen() successfully opened (but not yet been fclose() of the closed file.
Tips and Comments
Tip:feof() function is very useful for traversing data of unknown length.
Note:If the server does not close the connection opened by fsockopen(), feof() will keep waiting until the timeout returns TRUE. The default timeout limit is 60 seconds, and this value can be changed using stream_set_timeout().
Note:If the passed file pointer is invalid, it may fall into an infinite loop because EOF does not return TRUE.
Example
<?php $file = fopen("test.txt", "r"); //Output all 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.