Read a file backwards line by line using fseek

后端 未结 9 2053
温柔的废话
温柔的废话 2020-11-27 07:33

How do I read a file backwards line by line using fseek?

code can be helpful. must be cross platform and pure php.

many thanks in advance

regards

9条回答
  •  情话喂你
    2020-11-27 08:07

    I know this has been answered already but I found another, maybe faster, way.

    // Read last 5000 chars of 'foo.log' 
    
    if(file_exists('foo.log') && $file = fopen('foo.log', 'r')) {
        fseek($file, -5000, SEEK_END);
    
        $text = stream_get_line($file, 5000); 
    
        var_dump($text);
    
        fclose($file);
    }
    

提交回复
热议问题