Created
June 27, 2013 09:59
-
-
Save gdugas/5875341 to your computer and use it in GitHub Desktop.
basic tail method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tail($fh, $max_length=10) { | |
$tailed = array(); | |
fseek($fh, -2, SEEK_END); | |
$stop = ftell($fh) + 2; | |
while (ftell($fh) >= 0 && count($tailed) < $max_length) { | |
$c = fgetc($fh); | |
if ($c == PHP_EOL) { | |
$current = ftell($fh); | |
$start = $current; | |
fseek($fh, $start); | |
$line = fread($fh, $stop - $start); | |
array_unshift($tailed, $line); | |
$stop = $start; | |
fseek($fh, $current); | |
} | |
fseek($fh, ftell($fh) - 2, SEEK_SET); | |
} | |
return $tailed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment