note 62774 added to function.fgets
| From: | tavernadelleidee | Date: | Thu, 09 Mar 2006 11:44:02 +0000 |
| Subject: | note 62774 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105130@lists.php.net to get a copy of this message | ||
I think that the quickest way of read a (long) file with the rows in reverse order is
<?php
$myfile = 'myfile.txt';
$command = "tac $myfile > /tmp/myfilereversed.txt";
passthru($command);
$ic = 0;
$ic_max = 100; // stops after this number of rows
$handle = fopen("/tmp/myfilereversed.txt", "r");
while (!feof($handle) && ++$ic<=$ic_max) {
$buffer = fgets($handle, 4096);
echo $buffer."<br>";
}
fclose($handle);
?>
It echos the rows while it is reading the file so it is good for long files like logs.
Borgonovo
----
Server IP: 80.241.173.254
Probable Submitter: 212.90.17.98
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.fgets.php
Edit -- http://master.php.net/note/edit/62774
Del: integrated -- http://master.php.net/note/delete/62774/integrated
Del: useless -- http://master.php.net/note/delete/62774/useless
Del: bad code -- http://master.php.net/note/delete/62774/bad+code
Del: spam -- http://master.php.net/note/delete/62774/spam
Del: non-english -- http://master.php.net/note/delete/62774/non-english
Del: in docs -- http://master.php.net/note/delete/62774/in+docs
Del: other reasons-- http://master.php.net/note/delete/62774
Reject -- http://master.php.net/note/reject/62774
Search -- http://master.php.net/manage/user-notes.php