Created
March 7, 2016 17:25
-
-
Save eniuz/526d355e190a9aa52a3e to your computer and use it in GitHub Desktop.
find last line in csv found on stack
This file contains hidden or 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
function getLastLine($file, $bufferSize){ | |
$fp = fopen($file, 'r'); | |
$size = filesize($file); | |
$buffer = ''; | |
for ($pos = $size - $bufferSize; ; $pos -= $bufferSize) { | |
$pos = max(0, $pos); | |
fseek($fp, $pos); | |
$b = rtrim(fread($fp, $bufferSize)); | |
$nlPos = strrpos($b, "\n"); | |
if ($nlPos !== false) { | |
$buffer = substr($b, $nlPos) . $buffer; | |
break; | |
} | |
else { | |
$buffer = $b . $buffer; | |
} | |
if ($pos == 0) { | |
break; | |
} | |
} | |
return trim($buffer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment