php如何读取大文件的最后一行,file函数就不用了。

解决方案 »

  1.   

    可以用 fseek 指定最后位置,然后往前读。
    比如 fseek($fp,-1,SEEK_END);就跑到倒数第一行了
      

  2.   


    20130721          2.02M        
    20130722          2.02M    
    20130723          2.02M    
    20130724          2.02M   
    20130725          2.02M    
    20130726          2.02M    
    20130727          2.02M   
    20130728          2.02M    
    20130729          2.02M    
    20130730          2.02M   
    20130731          2.02M   
    20130801          2.02M     比如这样的我想去最后一行,具体代码怎么写,给个demo,谢谢!
      

  3.   

    $fn = '你的文本文件名';
    $fp = fopen($fn, 'r');
    fseek($fp, -1, SEEK_END);
    $s = '';
    while(($c = fgetc($fp)) !== false) {
      if($c == "\n" && $s) break;
      $s = $c . $s;
      fseek($fp, -2, SEEK_CUR);
    }
    fclose($fp);
    echo $s;
      

  4.   

    如果文件不是巨大,正读也很方便
    $fp = fopen($fn, 'r');
    while($buf = fgets($fp)) $res = $buf;
    fclose($fp);
    echo $res;