note 40015 deleted from function.fgets by nlopess
| From: | nlopess@php.net | Date: | Thu, 19 Feb 2004 13:40:07 +0000 |
| Subject: | note 40015 deleted from function.fgets by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65388@lists.php.net to get a copy of this message | ||
Note Submitter: bruce
----
Masar's version is definitely an improvement, but if there is no LF (assuming UNIX line
endings) at the end of the last line, this last line will be lost. Here's a new variant, which
should have output which exactly matches the input.
<?php
$handle = fopen("/tmp/inputfile.txt", "r");
if ($handle)
{
$buffer = fgets($handle, 4096);
while (!feof($handle))
{
echo $buffer;
$buffer = fgets($handle, 4096);
}
fclose($handle);
if (strlen($buffer))
echo $buffer;
}
?>
Other endings are of course possible, including:
<?php
// ...
if (strlen(trim($buffer)))
echo $buffer."\n";
?>