Logregels (oudste eerst) of lege array indien niet beschikbaar. */ public static function tail($lines = 100) { if (!self::$logFile || !file_exists(self::$logFile)) { return []; } $lines = max(1, (int)$lines); $handle = @fopen(self::$logFile, 'rb'); if ($handle === false) { return []; } if (fseek($handle, 0, SEEK_END) !== 0) { fclose($handle); return []; } $fileSize = ftell($handle); if ($fileSize === false || $fileSize === 0) { fclose($handle); return []; } $chunkSize = 8192; $position = $fileSize; $buffer = ''; $newlineCount = 0; // Read backwards until we have enough newlines or reach the start while ($position > 0 && $newlineCount <= $lines) { $readSize = (int)min($chunkSize, $position); $position -= $readSize; if (fseek($handle, $position, SEEK_SET) !== 0) { break; } $chunk = fread($handle, $readSize); if ($chunk === false) { break; } $buffer = $chunk . $buffer; $newlineCount = substr_count($buffer, "\n"); } fclose($handle); if ($buffer === '') { return []; } // Split while keeping the newline characters, matching file() behaviour $result = preg_split('/(?<=\n)/', $buffer, -1, PREG_SPLIT_NO_EMPTY); if ($result === false) { return []; } return array_slice($result, -$lines); } }