note 37147 added to function.fseek

From: Date: Tue, 04 Nov 2003 23:03:06 +0000
Subject: note 37147 added to function.fseek
Groups: php.notes 
Request: Send a blank email to php-notes+get-59911@lists.php.net to get a copy of this message
Here is a function that returns the last line of a file. This should be quicker than reading the whole file till you get to the last line. If you want to speed it up a bit, you can set the $pos = some number that is just greater than the line length. The files I was dealing with were various lengths, so this worked for me. function readlastline() { $fp = @fopen("/dosmnt/LOGFILE.DAT", "r"); $pos = -1; $t = " "; while ($t != "\n") { fseek($fp, $pos, SEEK_END); $t = fgetc($fp); $pos = $pos - 1; } $t = fgets($fp); fclose($fp); return $t; } ---- Manual Page -- http://www.php.net/manual/en/function.fseek.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37147 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37147&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37147&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#59911) next »