Why does feof return a different result when it is used with fseek and fread?

30 Views Asked by At

While debugging a function I am implementing, I noticed a different return result from feof when it is used with fseek or fread, would you explain to me why feof is unable to test the end of file when fseek get involved?

$file = fopen('data.txt', 'r');
// Do some operations on the file then seek to end of it
fseek($file, 0, SEEK_END);
echo ftell($file) . PHP_EOL;
var_dump(feof($file));
echo '##############################################################' . PHP_EOL;

rewind($file);
while(!feof($file))
{
    fread($file, 500);
}

echo ftell($file) . PHP_EOL;
var_dump(feof($file));
fclose($file);

Output:

3505
bool(false)
##############################################################
3505
bool(true)
0

There are 0 best solutions below