php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42175 feof() fails to detect the end of file
Submitted: 2007-08-02 10:23 UTC Modified: 2007-09-21 06:02 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: nikhil dot gupta at in dot ibm dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5CVS-2007-08-02 (snap) OS: Linux, Win32-xp
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nikhil dot gupta at in dot ibm dot com
New email:
PHP Version: OS:

 

 [2007-08-02 10:23 UTC] nikhil dot gupta at in dot ibm dot com
Description:
------------
feof() is not able to detect the end of file even when the file pointer is set to EOF using fseek() or any other ways. If we use the sample code similar to that given in the help docs for feof() on php.net:

<?php          
$fh = fopen("file.tmp", "w");
fclose($fh);
$fh = fopen("file.tmp", "r");
while(!feof()){
}
fclose($fh);
unlink("file.tmp");
?>

The code runs to endless loop as feof donot detect the EOF.

Reproduce code:
---------------
<?php          
$fh = fopen("file.tmp", "w");
fwrite($fh, "aaa");
fclose($fh);
$fh = fopen("file.tmp", "r");
var_dump( ftell($fh) );
var_dump( fseek($fh, 0, SEEK_END) );
var_dump( ftell($fh) );
var_dump( feof($fh) );
fclose($fh);
unlink("file.tmp");
?>

Expected result:
----------------
int(0)
int(0)
int(3)
bool(true)

Actual result:
--------------
int(0)
int(0)
int(3)
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-02 10:51 UTC] nikhil dot gupta at in dot ibm dot com
I observed that when I use fgetcsv() to read a file to end, and then check for EOF, feof() returns true. Hence I suspect the problem is with fseek() that although it succceeds in taking the file pointer to EOF but fails to set the EOF flag.
 [2007-09-19 14:57 UTC] tzachi at zend dot com
feof() of PHP is the same of feof in C, and i think that the best definition is this: (taken from http://msdn2.microsoft.com/en-us/library/xssktc6e(VS.71).aspx):
"The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return."

So if you add an additional fgetc(), it should work:
var_dump( ftell($fh) );
var_dump( fseek($fh, 0, SEEK_END) );
var_dump( ftell($fh) );
var_dump( fgetc($fh) );
var_dump( feof($fh) );
 [2007-09-21 06:02 UTC] nikhil dot gupta at in dot ibm dot com
Thanks for the explanation. I am cancelling the bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 09:01:26 2024 UTC