Created
January 8, 2020 15:39
-
-
Save miamibc/5da5a84cb0162529322ae87340d3e926 to your computer and use it in GitHub Desktop.
read last line(s) from very large file with php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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