php read large text file log
I have a text log file, about 600 MB.
I want to read it using php and display the data on a html page, but I only need the last 18 lines that were added each time I run the script.
Since its a large file, I can't read it all in then flip the array as I would have ho开发者_如何学编程ped. Is their another way?
Use fopen
, filesize
and fseek
to open the file and start reading it only near the end of the file.
Comments on the fseek
manual page include full code to read the last X lines of a large file.
Loading that size file into memory would probably not be a good idea. This should get you around that.
$file = escapeshellarg($file);
$line = 'tail -n 18 '.$file;
system($line);
you can stream it backwards with
$file = popen("tac $filename",'r');
while ($line = fgets($file)) {
echo $line;
}
The best way to do this is use fread and fgets to read line by line, this is extreamly fast as only one line is read at one time and not the while file:
Example of usage would be:
$handle = fopen("/logs/log.txt", "r")
if ($handle)
{
fseek($handle,-18,SEEK_END); //Seek to the end minus 18 lines
while (!feof($handle))
{
echo fgets($handle, 4096); //Make sure your line is less that 4096, otherwise update
$line++;
}
fclose($handle);
}
For the record, had the same problem and tried every solution here.
Turns out Dagon's popen "tac $filename
" way is the fastest and the one with the lowest memory and CPU loads.
Tested with a 2Gb log-file reading 500, 1000 and 2000 lines each time. Smooth. Thank you.
精彩评论