2

Looking for a PHP solution to:

  1. Perform 'tail' type shell command on a file
  2. Perform 'tac' type shell command on a file

In other words, display the last lines of a file in reverse order, from last to first, like a log reader.

I just asked this question for Bash, hoping I could run a shell_exec(), but the recommended method - tac - doesn't work on my OSX.

Community
  • 1
  • 1
Yarin
  • 159,198
  • 144
  • 384
  • 498

2 Answers2

3

Take a look at http://php.net/manual/en/function.array-reverse.php

A non-efficient way would be to fgets() the whole file into an array and then reverse it (Not good on big files).

OR

http://tekkie.flashbit.net/php/tail-functionality-in-php

Martin Samson
  • 3,860
  • 20
  • 25
0

You can get the functionality of tac on OSX by using the -r option to tail

tail -r file
Line3
Line2
Line1
Mark Setchell
  • 169,892
  • 24
  • 238
  • 370