The best way to do this is to use fread and fgets to read line by line, it is very fast, since only one line is read at a time, and not the while file:
Usage example:
$handle = fopen("/logs/log.txt", "r") if ($handle) { fseek($handle,-18,SEEK_END); //Seek to the end minus 18 lines while (!feof($handle)) { echo fgets($handle, 4096); //Make sure your line is less that 4096, otherwise update $line++; } fclose($handle); }
source share