// Created on savesnippets.com ยท https://savesnippets.com/EhnkyVUpAn7Xee 0 && $found <= $lines) { $read = min($chunk, $pos); $pos -= $read; fseek($fh, $pos); $buffer = fread($fh, $read) . $buffer; $found = substr_count($buffer, "\n"); } fclose($fh); $all = explode("\n", rtrim($buffer, "\n")); return array_slice($all, -$lines); } print_r(tail('/var/log/nginx/access.log', 5));