php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65603 feof not reporting end of file when using fread
Submitted: 2013-09-02 03:58 UTC Modified: 2013-09-02 04:55 UTC
From: grom358 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.5.3 OS: Linux
Private report: No CVE-ID: None
 [2013-09-02 03:58 UTC] grom358 at gmail dot com
Description:
------------
If using fread and have read the last byte in the file, feof still reports have 
not reached end of file.

Test script:
---------------
<?php
$fp = fopen('feof.bin', 'w');
fwrite($fp, 'abc');
fclose($fp);

$fp = fopen('feof.bin', 'r');
$bytes = fread($fp, 3);
echo feof($fp) ? 't' : 'f';
echo PHP_EOL;
fclose($fp);

unlink('feof.bin');

Expected result:
----------------
Expected feof to return true

Actual result:
--------------
feof returned false. 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-02 04:55 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2013-09-02 04:55 UTC] rasmus@php.net
feof() indicates that you have tried to read beyond the end of the file. For a 3-
byte file, if you only read 3 bytes you have not yet tried to read beyond the end 
of the file and thus feof() has not triggered yet. feof() cannot predict that the 
next read will fail. By the time you do the next read perhaps the file has grown. 
This is exactly how this function works in C as well which is where it comes 
from.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 13:01:30 2025 UTC