PHP - Returning the last line in a file?

后端 未结 9 1547
鱼传尺愫
鱼传尺愫 2020-11-30 09:46

I\'m guessing it\'s fgets, but I can\'t find the specific syntax. I\'m trying to read out (in a string I\'m thinking is easier) the last line added to a log file.

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 10:49

    function seekLastLine($f) {
        $pos = -2;
        do {
            fseek($f, $pos--, SEEK_END);
            $ch = fgetc($f);
        } while ($ch != "\n");
    }
    

    -2 because last char can be \n

提交回复
热议问题