The Problem
You want the functionality of tail(1) in a php function.
The Solution
Bellow is a php function that gives you the last N lines from a file. It doesn't do everything tail(1) does ( like following, retrying, etc ) but just the basic stuff:
function tail($file, $num_to_get=10) { $chunklen = 4096; $data="";$ret="";$lc=0; while($chunklen > 0) { for($i=$dl-1;$i>=0;$i--){ if($data[$i]=="\n"){ if($lc==0 && $ret!="")$lc++; $lc++; if($lc>$num_to_get)return $ret; } $ret=$data[$i].$ret; } if($position-$chunklen < =0 ){ $position = $position - $chunklen; } return $ret; }
It may not be the fastest solution but it works.
Have a better one? Please let me know about it.
I’ve written a small PHP tail -f class that is similar to this. You can view the source code at http://code.google.com/p/php-tail/