php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2954 feof doesn't always evaluate correctly
Submitted: 1999-12-10 13:47 UTC Modified: 1999-12-28 21:40 UTC
From: nickl at mycomputer dot com Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.12 OS: linux RH
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nickl at mycomputer dot com
New email:
PHP Version: OS:

 

 [1999-12-10 13:47 UTC] nickl at mycomputer dot com
If 'tmp' is a file that is exactly 25 bytes long, then: 

$fd = fopen('tmp', 'r');
echo "fpos: " . ftell($fd) .
     " eof: " . feof($fd) . "\n";
fread($fd, 25);
echo "fpos: " . ftell($fd) .
     " eof: " . feof($fd) . "\n";
fread($fd, 1);
echo "fpos: " . ftell($fd) .
     " eof: " . feof($fd) . "\n";

results:
                                                        fpos: 0 eof?:
fpos: 25 eof?:
fpos: 25 eof?: 1

This caused one of my scripts to crash since even though
the next fread() didn't die, passing the null it returned
on to an unpack() did.

My current work-around is rather than to use:

while (feof($fd)) {}

I do:

$size = filesize($file);
while (ftell($fd) < $size) {}

However, this obviously won't work on streams that don't come from the local filesystem.

My setup is: (from config.status)

./configure
--with-mysql=/usr/local/mysql
--enable-track-vars
--enable-sysvsem
--enable-sysvshm
--with-gdchart=/usr/local/src/gdchart0.94b --with-gd=/usr/local/src/gd1.5
--prefix=/usr/local
--with-ttf
--enable-magic-quotes
--with-apache=/usr/local/apache

Actually, this isn't exactly correct - I also
compile the CGI version - for which I remove
the apache stuff... and in fact it's the CGI
version that I was using when I noticed the
problem.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-12-10 13:49 UTC] nickl at mycomputer dot com
s/while(feof($fd))/while(!feof($fd))/

 [1999-12-28 21:40 UTC] sas at cvs dot php dot net
The function feof reports the status of the end-of-file indicator. This indicator is only set, if the fread function has noticed that the stream of data has no more data available. Therefore, the observed behaviour of feof is correct.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Jul 09 11:00:01 2026 UTC