Tail In PHP

December 3, 2010

So I’m working on a little admin interface and I decided to tail some logs. It’s in PHP and Google came up with some stuff, but they were all a bit finickey and not well documented. So I wrote my own!

Here’s the basic concept:

  1. Open File
  2. Seek To End
  3. Loop:
    1. Seek Back By Chunk Size
    2. Read Chunk
    3. Count Newlines
    4. Stash In Buffer
    5. Break If Enough Newlines Total
  4. Trim Extra Line & Cruft
  5. Done!

Here’s the code:

You can give it a try on some junk data here: http://static.velvetcache.org/pages/2010/12/03/tail-in-php/

Categories: Geek
Tags: , , , , ,

Leave A Comment

Your email will not be published.