|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-11-07 10:38 UTC] dsp at tdcspace dot dk
Description:
------------
I really thought that the feof() was TRUE when the logical file pointer is a EOF.
but no !
we need to do fread() and get an empty record before the feof() reports TRUE.
sunday is the last day in a week and thus end-of-week - and so is the last byte a the file is also EOF.
but the way feof() works - is says - it must wait until monday - before it can find out !
Reproduce code:
---------------
// open and read binary file with 128 byte records
$h = fopen("phpbugs.dat", "rb");
while(!feof($h)) {
$rec = fread($h, 128);
if ($strlen($rec) == 0) break; // should not be needed
}
close($h);
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 18 04:00:01 2025 UTC |
For me, the feof() also works badly. while($max && !feof($src)) { $l=$max>0x100000 ? 0x100000 : $max; $s=fread($src,$l); if($s===false || $s==='') return 1; if(fwrite($dst,$s)!==strlen($s)) return 2; if(strlen($s)!==$l) break; $max-=$l; } In this code the "$str===false || $str===''" should be not reachable without file problem but it is reachable on Linux. WHY??? If the function (feof) works correctly (IMO it doesn't work correctly due to manual definition) - HOW TO CHECK WHY I CANNOT READ FILE? Because of permisions, or file delete OR real end of file? PHP 7.4, Linux. I believe there is real problem in feof implementation in PHP.Some people don't understand how eof works. `eof` is the end of file (stream) byte. To understand that the end of the file (stream) has been reached while reading, this byte must be `read (fread ($ res, 1);` or `fgetc ($ res));` Even when you move the pointer with `fseek ($ res, $ offset)`, you won't determine the end of the file until you read the eof byte. public function isNextEof($res,int $size=1):bool { $tell=ftell($res); $check=$this->fread($res,$size); $eof=feof($res); fseek($res,$tell); return $eof; } Be sure to ask the error to find out why file functions return false or empty strings. https://www.php.net/manual/en/function.socket-last-error.php https://www.php.net/manual/en/function.error-get-last.php Please place this text in coments https://www.php.net/manual/en/function.feof.php (in my case, the provider's IP is for some reason in the black list)Minor edits to my previous comment. `eof` it the end file byte; Editing the code public function isNextEof($res,int $size=1):bool { $tell=ftell($res); //$check=fread($res,$size); fseek($res,$size-1,SEEK_CUR); $check=fgetc($res); $eof=feof($res); fseek($res,$tell); return $eof; }