前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[PHP] 读取大文件并显示

[PHP] 读取大文件并显示

作者头像
唯一Chat
发布2019-09-10 16:37:53
1.1K0
发布2019-09-10 16:37:53
举报
文章被收录于专栏:陶士涵的菜地陶士涵的菜地

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

PHP代码:

代码语言:javascript
复制
<?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['action'])){
            self::$_GET['action']();
            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='red'>".$line."</font>";
            }
            $html.="<div class='line'>".$line."<div>";
        }
        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='red'>".$line."</font>";
            }
            $html.="<div class='line'>".$line."<div>";
        }
        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>
<div class="logsBox apache">
    <div class="line">日志读取...</div>
</div>
<div class="logsBox nginx">
    <div class="line">日志读取...</div>
</div>
<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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-07-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档