note 37147 modified in function.fseek by aidan

From: Date: Tue, 29 Jun 2004 18:23:35 +0000
Subject: note 37147 modified in function.fseek by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72120@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. <?php function readlastline($file) { $fp = @fopen($file, "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; } ?> --was-- 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; } http://php.net/manual/en/function.fseek.php

« previous php.notes (#72120) next »