Through Shell
, I can display the last 5 lines of a file using the tail
command.
So:
> tail -n 5 public/index.php
I'd like to do the same thing in PHP. But I did not want to load the whole file, but actually only display the last 5 lines.
I know you can display the first 5 like this:
$f = new SplFileObject('public/index.php');
$f->rewind();
for ($i = 0; $i < 5; $i++) {
echo $f->fgets();
}
But how can I display the last 5 lines of a file while maintaining good performance?
Note : I do not want a solution where you have to open the command line via PHP.