Skip to content

Instantly share code, notes, and snippets.

@gdugas
Created June 27, 2013 09:59
Show Gist options
  • Save gdugas/5875341 to your computer and use it in GitHub Desktop.
Save gdugas/5875341 to your computer and use it in GitHub Desktop.
basic tail method
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