Reading a large php text file

I have a text log file, about 600 MB.

I want to read it with php and display the data on the html page, but I only need the last 18 lines that were added every time the script was run.

Since its a large file, I cannot read it all and then flip the array, as I would hope. Is this their other way?

+2
source share
5 answers

Use fopen , filesize and fseek to open the file and start reading it only at the end of the file.

The comments on the fseek page include the complete code for reading the last X lines of a large file.

+2
source

Loading this size file into memory is probably not a good idea. This should help you with that.

 $file = escapeshellarg($file); $line = 'tail -n 18 '.$file; system($line); 
+1
source

you can pass it back with

 $file = popen("tac $filename",'r'); while ($line = fgets($file)) { echo $line; } 
+1
source

The best way to do this is to use fread and fgets to read line by line, it is very fast, since only one line is read at a time, and not the while file:

Usage example:

 $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); } 
0
source

There was the same problem for the record and you can try every solution.

Turns off Dagon popen "tac $filename ", the fastest and the one with the lowest memory and CPU usage.

Tested with 2Gb log file, reading 500, 1000 and 2000 lines each time. Smooth; smooth. Thanks.

0
source

Source: https://habr.com/ru/post/1446058/


All Articles