Reading a file backwards in PHP

February 28th, 2008

This morning I needed to read from a file line by line from the bottom. In PHP. Perl, of course, has a module to do this. A quick view source decided that I didn’t want to get into file seeks before breakfast. Very happy with my solution:

$file = popen("tac $filename",'r');

while ($line = fgets($file)) {
  echo $line;

3 Responses to “Reading a file backwards in PHP”

  1. Marek says:

    Thanks for this post, came very handy.

  2. Filipe says:

    Great, very handy indeed :)

  3. excellent solution … thx a lot!