php read large text file log

后端 未结 5 1733
时光说笑
时光说笑 2020-12-10 22:04

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

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 22:20

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

提交回复
热议问题