Reading large files from end

前端 未结 10 1556
慢半拍i
慢半拍i 2020-12-28 19:47

Can I read a file in PHP from my end, for example if I want to read last 10-20 lines?

And, as I read, if the size of the file is more than 10mbs I start getting erro

10条回答
  •  情歌与酒
    2020-12-28 20:26

    You can use fopen and fseek to navigate in file backwards from end. For example

    $fp = @fopen($file, "r");
    $pos = -2;
    while (fgetc($fp) != "\n") {
        fseek($fp, $pos, SEEK_END);
        $pos = $pos - 1;
    }
    $lastline = fgets($fp);
    

提交回复
热议问题