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

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

WBOY
WBOYOriginal
2016-06-23 14:17:46887browse

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


回复讨论(解决方案)

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

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,谢谢!

$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;

如果文件不是巨大,正读也很方便

$fp = fopen($fn, 'r');while($buf = fgets($fp)) $res = $buf;fclose($fp);echo $res;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn