Skip to content

Instantly share code, notes, and snippets.

@miamibc
Created January 8, 2020 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miamibc/5da5a84cb0162529322ae87340d3e926 to your computer and use it in GitHub Desktop.
Save miamibc/5da5a84cb0162529322ae87340d3e926 to your computer and use it in GitHub Desktop.
read last line(s) from very large file with php
/**
* read last line(s) from very large file
*
* @param string $filename filename to open
* @param integer $buffer Perfoorm search of last line in [buffer] bytes got from end of file
*
* @return string
*/
function read_last_line(string $filename, int $buffer = 1024*1024 )
{
$fp = fopen( $filename, 'r');
fseek($fp, -$buffer, SEEK_END);
$lines = [];
while (($line = fgets($fp) ) !== false)
{
$lines[] = rtrim($line,"\r\n");
}
fclose($fp);
return end($lines);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment