note 15515 added to function.fgets
| From: | reinke at e-softinc dot com | Date: | Fri, 14 Sep 2001 19:45:37 +0000 |
| Subject: | note 15515 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-18027@lists.php.net to get a copy of this message | ||
The given example for reading a file is incorrect. It will introduce an
additional run through the loop, since once the last byte has been
read of a text file (terminating say in \n), the feof does not yet indicate
end of file - you have to read PAST it.
The correct code structure looks more like this:
$fd = fopen ("/tmp/inputfile.txt", "r");
$buffer = fgets($fd, 4096);
while (!feof ($fd)) {
echo $buffer;
$buffer = fgets($fd, 4096);
}
fclose ($fd);
http://www.php.net/manual/en/function.fgets.php