PHP 读取大文件并显示的简单实例(推荐) 使用PHP读取日志文件,当文件比较大的时候,会报内存不足,因此应该部分读取,读取指定的行数的数据 PHP代码: readLogs(self::LOG_PATH,self::PAGES); $html=""; foreach($result as $line){ if(strpos($line,"error:")){ $line="".$line.""; } $html.="
".$line."
"; } echo $html; } public static function showNginxLogs(){ $test=new Test(); $result=$test->readLogs(self::NGINX_LOG_PATH,self::PAGES); $html=""; foreach($result as $line){ if(strpos($line,"error")){ $line="".$line.""; } $html.="
".$line."
"; } echo $html; } /** * 读取日志 */ private function readLogs($filePath,$num=20){ $fp = fopen($filePath,"r"); $pos = -2; $eof = ""; $head = false; //当总行数小于Num时,判断是否到第一行了 $lines = array(); while($num>0){ while($eof != "\n"){ if(fseek($fp, $pos, SEEK_END)==0){ //fseek成功返回0,失败返回-1 $eof = fgetc($fp); $pos--; }else{ //当到达第一行,行首时,设置$pos失败 fseek($fp,0,SEEK_SET); $head = true; //到达文件头部,开关打开 break; } } array_unshift($lines,fgets($fp)); if($head){ break; } //这一句,只能放上一句后,因为到文件头后,把第一行读取出来再跳出整个循环 $eof = ""; $num--; } fclose($fp); return array_reverse($lines); } } Test::main(); ?>
日志读取...
日志读取...
以上这篇PHP 读取大文件并显示的简单实例(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持中文源码网。