Read last line of a text file.
<?php
$logFile = fopen('test.txt', 'r');
$lastLine = '';
if ($logFile) {
$cursor = -1;
fseek($logFile, $cursor, SEEK_END);
$char = fgetc($logFile);
// trim trailing newline chars of the file
while ($char === "\n" || $char === "\r") {
fseek($logFile, $cursor--, SEEK_END);
$char = fgetc($logFile);
}
// read until the start of file or first newline char
while ($char !== false && $char !== "\n" && $char !== "\r") {
$lastLine = $char . $lastLine;
fseek($logFile, $cursor--, SEEK_END);
$char = fgetc($logFile);
}
}