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
  • 173,523
  • 149
  • 402
  • 512

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,970
  • 21
  • 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
  • 191,897
  • 31
  • 273
  • 432