/** * Method to tail (a few last rows) of a file. * * @param $filename * @param int $lines * @param int $buffer * * @return string */ public function tail($filename, $lines = 10, $buffer = 4096) { $f = fopen($filename, 'rb'); $output = ''; fseek($f, -1, SEEK_END); if (" " != fread($f, 1)) { --$lines; } while (ftell($f) > 0 && $lines >= 0) { $seek = min(ftell($f), $buffer); fseek($f, -$seek, SEEK_CUR); $output = ($chunk = fread($f, $seek)).$output; fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); $lines -= substr_count($chunk, " "); } while ($lines++ < 0) { $output = substr($output, strpos($output, " ") + 1); } fclose($f); return $output; }`