In PHP programming, it is common to need to read the last few lines of a large file, especially when working with log files or other types of record files. To improve performance and avoid loading the entire file into memory, we need to adopt a more efficient reading method.
The traditional approach is to use the file function to load the entire file into memory, convert it into an array, and then use array operations to get the last n lines. However, this method is inefficient for large files because it consumes a lot of memory.
Therefore, we need a more memory-efficient and faster way to read the last n lines of a file.
To improve efficiency, we can use PHP's fseek and fgetc functions to incrementally read the last n lines of a large file. The steps are as follows:
First, we open the file using fopen and use fseek to position the file pointer at the end of the file.
$file = "path/to/file.txt";
$file_handle = fopen($file, "r");
fseek($file_handle, 0, SEEK_END);
Next, we use fgetc to read each character of the file backward until we encounter a newline character. When we find a newline, it means we have read a complete line. We save the line in an array and continue reading backward until we've read n lines.
$lines = [];
$n = 10;
$pos = ftell($file_handle);
while ($n > 0 && $pos) {
fseek($file_handle, $pos);
if (($char = fgetc($file_handle)) === "\n") {
array_unshift($lines, fgets($file_handle));
$n--;
}
$pos--;
}
In this process, we read one character at a time and when we encounter a newline, we consider it the end of one line. Each time we read a line, we insert it at the beginning of the array and decrement n until we've read the desired number of lines.
By using fseek and fgetc functions, we can efficiently read the last n lines of a large file. Compared to loading the entire file into memory, this method saves memory and significantly improves processing speed. It is especially suitable when only a few lines from the end of the file are needed, making it an ideal choice when handling large files.