Home  >  Article  >  Backend Development  >  PHP implementation of reading large files and displaying them

PHP implementation of reading large files and displaying them

墨辰丷
墨辰丷Original
2018-06-01 15:27:211589browse

本篇文章主要介绍PHP实现读取大文件并显示的实例,感兴趣的朋友参考下,希望对大家有所帮助。

使用PHP读取日志文件,当文件比较大的时候,会报内存不足,因此应该部分读取,读取指定的行数的数据

PHP代码:

<?php
class Test{
  //日志路径
  const LOG_PATH="E:\phpServer\Apache\logs\error.log";
  const NGINX_LOG_PATH="E:\phpServer\\nginx\logs\error.log";
  //显示的行数
  const PAGES=50;
  public static function main(){
    header("content-type:text/html;charset=utf-8");
    
    if(!empty($_GET[&#39;action&#39;])){
      self::$_GET[&#39;action&#39;]();
      exit;
    }
  }

  public static function showApacheLogs(){
    $test=new Test();
    $result=$test->readLogs(self::LOG_PATH,self::PAGES);
    $html="";
    foreach($result as $line){
      if(strpos($line,"error:")){
        $line="<font color=&#39;red&#39;>".$line."</font>";
      }
      $html.="<p class=&#39;line&#39;>".$line."<p>";
    }
    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="<font color=&#39;red&#39;>".$line."</font>";
      }
      $html.="<p class=&#39;line&#39;>".$line."<p>";
    }
    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();
?>
<style type="text/css">
*{
  padding: 0;
  margin: 0;
}
.logsBox{
  margin:5px;
  padding: 5px;
  width: 600px;
  background: #000;
  color:#fff;
  font-size: 13px;
  float: left;
}
.logsBox .line{
  margin: 12px 0;
}
</style>
<p class="logsBox apache">
  <p class="line">日志读取...</p>
</p>
<p class="logsBox nginx">
  <p class="line">日志读取...</p>
</p>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){

    function showLogs(api,showClass){
      function readLogs(){
        $.ajax({
          url:api,
          type:"get",
          dataType:"text",
          success:function(data){
            $(showClass).html(data);
          }
        });
      }
      readLogs();
      setInterval(readLogs,5000);
    }
    showLogs("?action=showNginxLogs",".nginx");
    showLogs("?action=showApacheLogs",".apache");
  });
</script>

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

thinkPHP模板中函数的使用方法

php+mysql实现登录注册修改密码网页

php 禁用eval() 函数的方法实例详解

The above is the detailed content of PHP implementation of reading large files and displaying them. For more information, please follow other related articles on the PHP Chinese website!

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