PHP : lire les dernières lignes d’un fichier texte
Très rapide et n’explose pas la mémoire,
sur un dump mysql de 300 Mo c’est environ 5 sec
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * * @param string $filename * @param integer $lines * @return array */ function read_last_log($filename, $lines) { if (!file_exists($filename)) return array('log no exist!') ; $fp = fopen((string)$filename,"r"); $result = array(); while (!feof($fp)) { $result[] = fgets($fp, 1024); if (count($result) > (int)$lines) { $result = array_slice($result, 1); } } fclose($fp); return $result; } |






