PHP: Tail tackling large files

Needed a function that could get me the last N lines of a log file. Wanted it to be efficient and not dependent on anything other than my code.

Found some versions, but they were either a bit messy or depended on unstable arithmetic (where filesize is greater than PHP_INT_MAX). So, I decided to take on the challenge and try to write one myself. Nice little exercise 🙂

My result looks a bit long, but that’s mostly because I have added lots of comments here so you can hopefully understand what’s going on 😛

function tail($filename, $lines = 10, $buffer = 4096)
    // Open the file
    $f = fopen($filename, "rb");

    // Jump to last character
    fseek($f, -1, SEEK_END);

    // Read it and adjust line number if necessary
    // (Otherwise the result would be wrong if file doesn't end with a blank line)
    if(fread($f, 1) != "\n") $lines -= 1;

    // Start reading
    $output = '';
    $chunk = '';

    // While we would like more
    while(ftell($f) > 0 && $lines >= 0)
        // Figure out how far back we should jump
        $seek = min(ftell($f), $buffer);

        // Do the jump (backwards, relative to where we are)
        fseek($f, -$seek, SEEK_CUR);

        // Read a chunk and prepend it to our output
        $output = ($chunk = fread($f, $seek)).$output;

        // Jump back to where we started reading
        fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);

        // Decrease our line counter
        $lines -= substr_count($chunk, "\n");

    // While we have too many lines
    // (Because of buffer size we might have read too many)
    while($lines++ < 0)
        // Find first newline and remove all text before that
        $output = substr($output, strpos($output, "\n") + 1);

    // Close file and return
    return $output;


You can try out a slightly modified version at Of course you won’t be able to try it on your multi-gigabyte-sized log file, but at least you can see if it does what it should 🙂

As usual, if you have any corrections or feedback, if you found it useful, please leave a comment!

  • Lorenzo Stanco

    Hi Torleif. Your post and your great algorithm was one (if not the main) source I used to draft this mini-article I posted as a self-question on Stack Overflow:

    What is the best way in PHP to read last lines from a file?

    In my own answer I tried to analyze all different methods for reading last lines from a file using PHP, with a special care about performance on different scenarios. Yours is the best one I found during my researches, have a look!

    Hope it helps.

    • Cool! What exactly did you change? Was it only the buffer size?

      • Lorenzo Stanco

        Yes, that’s the only thing I changed. I noticed that when reading a few lines a reduced buffer (4096 bytes is quite large if not reading lot of lines) can improve performances. That’s all, your algorithms is surely the best I’ve seen during my researches.

  • mauro

    Great script!

  • Pingback: PHP从文件中读取最后一行的最好方法是什么? – CodingBlog()