note 69008 added to function.fseek
| From: | marc dot roe at gmail dot com | Date: | Sat, 19 Aug 2006 16:38:10 +0000 |
| Subject: | note 69008 added to function.fseek | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116081@lists.php.net to get a copy of this message | ||
I tried to improve and modify (mail at ulf-kosack dot de)'s function. Actually it is very fast,
i.e. requires much less time than to get the last five, ten or whatever lines of a file using file()
ore file_get_contents().
function read_file($file, $lines)
{
$handle = fopen($file, "r");
$linecounter = $lines;
$pos = -2;
$beginning = false;
$text = array();
while ($linecounter > 0) {
$t = " ";
while ($t != "\n") {
if(fseek($handle, $pos, SEEK_END) == -1) {
$beginning = true; break; }
$t = fgetc($handle);
$pos --;
}
$linecounter --;
if($beginning) rewind($handle);
$text[$lines-$linecounter-1] = fgets($handle);
if($beginning break;
}
fclose ($handle);
return array_reverse($text); // array_reverse is optional: you can also just return the $text
array which consists of the file's lines.
}
The good thing now is, that you don't get an error when your requesting more lines than the
file contains. In this case the function will just return the whole file content.
----
Server IP: 194.153.94.10
Probable Submitter: 213.200.229.203
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.fseek.php
Edit -- https://master.php.net/note/edit/69008
Del: integrated -- https://master.php.net/note/delete/69008/integrated
Del: useless -- https://master.php.net/note/delete/69008/useless
Del: bad code -- https://master.php.net/note/delete/69008/bad+code
Del: spam -- https://master.php.net/note/delete/69008/spam
Del: non-english -- https://master.php.net/note/delete/69008/non-english
Del: in docs -- https://master.php.net/note/delete/69008/in+docs
Del: other reasons-- https://master.php.net/note/delete/69008
Reject -- https://master.php.net/note/reject/69008
Search -- https://master.php.net/manage/user-notes.php