Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Slightly modified version of http://www.geekality.net/2011/05/28/php-tail-tackling-large-files/
* @author Torleif Berger, Lorenzo Stanco
* @link http://stackoverflow.com/a/15025877/995958
* @license http://creativecommons.org/licenses/by/3.0/
*/
function tailCustom($filepath, $lines = 1, $adaptive = true) {
// Open file
$f = @fopen($filepath, "rb");
if ($f === false) return false;
// Sets buffer size, according to the number of lines to retrieve.
// This gives a performance boost when reading a few lines from the file.
if (!$adaptive) $buffer = 4096;
else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
// 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
fclose($f);
return trim($output);
}
?>
@mookman288

This comment has been minimized.

Copy link

@mookman288 mookman288 commented Aug 7, 2015

Wouldn't using PHP_EOL be more effective when dealing with cross platform support?

@igorpan

This comment has been minimized.

Copy link

@igorpan igorpan commented Aug 31, 2015

@mookman288 No, because you don't know on which platform file was written

@bkdotcom

This comment has been minimized.

Copy link

@bkdotcom bkdotcom commented Oct 20, 2015

@igorpan I'd say it's safe to say that 99 times out of 100 the file we're tailing was written by the current file system . Either way, assumptions are being made regarding the line ending.

@heathermeeker

This comment has been minimized.

Copy link

@heathermeeker heathermeeker commented Sep 15, 2016

Thanks for making this function available. Could you please apply a license (like BSD/MIT) to it? Or point to the license if it is posted elsewhere.

@lorenzos

This comment has been minimized.

Copy link
Owner Author

@lorenzos lorenzos commented Sep 20, 2016

Sorry everyone for not replying, it looks like GitHub does not notify me for comments on gists.

@mookman288 @bkdotcom Searching for "\n" should work both in Linux and Windows, because the latter write line ends as "\r\n".

@heathermeeker I didn't apply any license to the code: feel free to use it. The original code from which this version is derived is here (as stated in my Stack Overflow answer), if you dig a bit you can find it's licensed with Creative Commons Attribution 3.0. Then, I'll say, just give credit to him in a comment in the source, and you'll be ok. I just made a revision adding all that infos in the gist itself.

@Djarnix

This comment has been minimized.

Copy link

@Djarnix Djarnix commented Sep 30, 2016

thanks lorenzos, it's working like a charm and added to my permanent library :)

@gaspaonrocks

This comment has been minimized.

Copy link

@gaspaonrocks gaspaonrocks commented Mar 21, 2018

Well... I added it to a project for a client of mine and it works wonderfully. Thank you very much @lorenzos

@modExpert

This comment has been minimized.

Copy link

@modExpert modExpert commented Jun 23, 2019

Sorry for naive question...
What happens when, during the execution of this code, an attempt is made to write to the log?

@mxdpeep

This comment has been minimized.

Copy link

@mxdpeep mxdpeep commented Oct 3, 2019

Sorry for naive question...
What happens when, during the execution of this code, an attempt is made to write to the log?

you must open the file in LOCK exclusive mode then or make a copy (if it's possible)

https://www.w3schools.com/php/func_filesystem_flock.asp

@consatan

This comment has been minimized.

Copy link

@consatan consatan commented Sep 29, 2020

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

Why not use fseek($f, -$seek, SEEK_CUR);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment