feof
Checks whether the end of a file has been reached.
Syntax
feof(resource $stream): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $stream | resource | An open file. |
Return value
bool`true` if there is nothing left to read.
Examples
Reading in chunks until the end of the file:
<?php
$file = sys_get_temp_dir() . '/ref-feof.txt';
file_put_contents($file, 'abcdefghij');
$handle = fopen($file, 'r');
while (!feof($handle)) {
echo '[', fread($handle, 4), ']';
}
fclose($handle);Output:
[abcd][efgh][ij]Notes
For line-by-line reading the condition ($line = fgets($handle)) !== false is more reliable: with feof() it is easy to get an extra empty line at the end.
See also
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.