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); } }