feof()
The feof() function in PHP is used to check whether the end of a file has been reached. It's a crucial function for server administrators and web developers who
Introduction to PHP feof() Function
The feof() function in PHP checks whether the end of file (EOF) has been reached on an open file pointer. As you read through a file with functions like fgets() or fread(), PHP keeps an internal cursor that advances with every read. feof() reports true once a read has tried to move past the last byte.
This page covers the syntax of feof(), the one subtlety that trips most people up (when exactly it returns true), the recommended loop pattern, and a common mistake to avoid.
If you are new to opening and reading files, start with Open and read a file in PHP.
Syntax
The syntax of the feof() function is as follows:
The PHP syntax of feof()
bool feof ( resource $stream )$stream: the file pointer (a resource) to check.- Returns:
bool—trueif the file pointer is at the end of the file or an error occurred,falseotherwise.
Parameters
The feof() function takes one required parameter:
$stream: The file pointer you want to check. It must be a valid resource opened withfopen()(or a similar stream function) and still open — passing a pointer that has already beenfclose()d, or anything that is not a resource, raises a warning.
How EOF is detected (the key gotcha)
feof() does not look ahead. It only returns true after a read function such as fgets() or fread() has already attempted to read past the end of the file:
- Checking
feof()immediately afterfopen(), before any read, always returnsfalse— even on an empty file. - A file that ends with a newline is fully consumed only when the read after the last line returns nothing; that final read is what flips
feof()totrue.
Because of this, the robust way to read a file is to test the return value of the read function, not feof() alone. On a non-seekable stream (a network socket or pipe) feof() can also return true only after the connection is closed, so relying on the read result is safer there too.
Examples
Here are some examples of how to use the feof() function:
Example 1: Check if the end of a file has been reached after a read
The following example reads the first line of the file, then checks if the end has been reached:
Check if the end of a file has been reached in PHP
<?php
$fileHandle = fopen("example.txt", "r");
// Read the first line
$firstLine = fgets($fileHandle);
// Now check if we've reached the end
if (feof($fileHandle)) {
echo "End of file reached";
} else {
echo "End of file not reached";
}
fclose($fileHandle);
?>Example 2: Standard loop pattern (recommended)
The cleanest, safest way to read a file line by line is to loop on the return value of fgets() — it returns false at end of file, so you do not even need to call feof() explicitly. This pattern also avoids the infinite-loop trap described below:
Standard read loop in PHP
<?php
$fileHandle = fopen("example.txt", "r");
if ($fileHandle === false) {
die("Error: Could not open file.");
}
while (($line = fgets($fileHandle)) !== false) {
echo $line;
}
fclose($fileHandle);
?>This prints each line of the file until the end is reached.
Example 3: A self-contained, runnable demo
This example creates a temporary file, writes three lines, then reads it back with feof() so you can see exactly when EOF flips to true:
Demonstrating feof() behavior
<?php
$path = tempnam(sys_get_temp_dir(), "demo");
file_put_contents($path, "line 1\nline 2\nline 3\n");
$fh = fopen($path, "r");
var_dump(feof($fh)); // false — nothing read yet
while (!feof($fh)) {
$line = fgets($fh);
if ($line === false) {
break; // read failed / past EOF
}
echo "Read: " . trim($line) . "\n";
}
var_dump(feof($fh)); // true — last read passed EOF
fclose($fh);
unlink($path);
?>Expected output:
bool(false)
Read: line 1
Read: line 2
Read: line 3
bool(true)Common mistake: the infinite loop
Calling fgets() without checking its return value inside a while (!feof($fh)) loop is risky. If a read error occurs (or the stream never reports EOF), feof() never becomes true and the loop spins forever. Always combine the EOF check with the read result — or, simpler, loop directly on fgets() as in Example 2.
Related functions
fopen()— open the file and get the pointerfeof()checks.fgets()— read one line at a time.fread()— read a fixed number of bytes.fclose()— close the pointer when you are done.
Conclusion
The feof() function reports whether a read has passed the end of an open file. The single most important rule is that it only turns true after a read attempt, so the safest file loops test the return value of fgets() or fread() directly. With that pattern you avoid both premature exits and infinite loops.