ID: 42175 Comment by: tzachi at zend dot com Reported By: nikhil dot gupta at in dot ibm dot com Status: Open Bug Type: Filesystem function related Operating System: Linux, Win32-xp PHP Version: 5CVS-2007-08-02 (snap) New Comment:
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) ); Previous Comments: ------------------------------------------------------------------------ [2007-08-02 10:51:53] 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-08-02 10:23:45] 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) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42175&edit=1