Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

feof

Checks whether the end of a file has been reached.

Syntax

feof(resource $stream): bool

Parameters

ParameterTypeDescription
$streamresourceAn 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

  • fgets — Reads one line from an open file.
  • fopen — Opens a file for reading or writing piece by piece.
  • fread — Reads a number of bytes from an open file.

In the tutorial

  • Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.

Thanks for reading!

Donate to the site — we work hard for you.