首页 > php教程 > php手册 > [PHP] 读取大文件并显示

[PHP] 读取大文件并显示

WBOY
发布: 2016-08-04 08:53:18
原创
1216 人浏览过

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

PHP代码:

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

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> showApacheLogs(){
        </span><span style="color: #800080;">$test</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Test();
        </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$test</span>->readLogs(self::LOG_PATH,self::<span style="color: #000000;">PAGES);
        </span><span style="color: #800080;">$html</span>=""<span style="color: #000000;">;
        </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$result</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$line</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$line</span>,"error:"<span style="color: #000000;">)){
                </span><span style="color: #800080;">$line</span>="<font color="red">".<span style="color: #800080;">$line</span>."</font>"<span style="color: #000000;">;
            }
            </span><span style="color: #800080;">$html</span>.="<div class="line">".<span style="color: #800080;">$line</span>."<div>"<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$html</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> showNginxLogs(){
        </span><span style="color: #800080;">$test</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Test();
        </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$test</span>->readLogs(self::NGINX_LOG_PATH,self::<span style="color: #000000;">PAGES);
        </span><span style="color: #800080;">$html</span>=""<span style="color: #000000;">;
        </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$result</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$line</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$line</span>,"error"<span style="color: #000000;">)){
                </span><span style="color: #800080;">$line</span>="<font color="red">".<span style="color: #800080;">$line</span>."</font>"<span style="color: #000000;">;
            }
            </span><span style="color: #800080;">$html</span>.="<div class="line">".<span style="color: #800080;">$line</span>."<div>"<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$html</span><span style="color: #000000;">;
    }
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * 读取日志
    </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> readLogs(<span style="color: #800080;">$filePath</span>,<span style="color: #800080;">$num</span>=20<span style="color: #000000;">){
        </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$filePath</span>,"r"<span style="color: #000000;">);
        </span><span style="color: #800080;">$pos</span> = -2<span style="color: #000000;">;  
        </span><span style="color: #800080;">$eof</span> = ""<span style="color: #000000;">;  
        </span><span style="color: #800080;">$head</span> = <span style="color: #0000ff;">false</span>;   <span style="color: #008000;">//</span><span style="color: #008000;">当总行数小于Num时,判断是否到第一行了  </span>
        <span style="color: #800080;">$lines</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();  
        </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$num</span>>0<span style="color: #000000;">){  
            </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$eof</span> != "\n"<span style="color: #000000;">){  
                </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">fseek</span>(<span style="color: #800080;">$fp</span>, <span style="color: #800080;">$pos</span>, SEEK_END)==0){    <span style="color: #008000;">//</span><span style="color: #008000;">fseek成功返回0,失败返回-1  </span>
                    <span style="color: #800080;">$eof</span> = <span style="color: #008080;">fgetc</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">);  
                    </span><span style="color: #800080;">$pos</span>--<span style="color: #000000;">;  
                }</span><span style="color: #0000ff;">else</span>{                               <span style="color: #008000;">//</span><span style="color: #008000;">当到达第一行,行首时,设置$pos失败  </span>
                    <span style="color: #008080;">fseek</span>(<span style="color: #800080;">$fp</span>,0,<span style="color: #000000;">SEEK_SET);  
                    </span><span style="color: #800080;">$head</span> = <span style="color: #0000ff;">true</span>;                   <span style="color: #008000;">//</span><span style="color: #008000;">到达文件头部,开关打开  </span>
                    <span style="color: #0000ff;">break</span><span style="color: #000000;">;  
                }  
                  
            }  
            </span><span style="color: #008080;">array_unshift</span>(<span style="color: #800080;">$lines</span>,<span style="color: #008080;">fgets</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">));  
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$head</span>){ <span style="color: #0000ff;">break</span>; }                 <span style="color: #008000;">//</span><span style="color: #008000;">这一句,只能放上一句后,因为到文件头后,把第一行读取出来再跳出整个循环  </span>
            <span style="color: #800080;">$eof</span> = ""<span style="color: #000000;">;  
            </span><span style="color: #800080;">$num</span>--<span style="color: #000000;">;  
        }  
        </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">);  
        </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">array_reverse</span>(<span style="color: #800080;">$lines</span><span style="color: #000000;">);  
    }
}
Test</span>::<span style="color: #000000;">main();
</span>?>
<style type="text/css">
*<span style="color: #000000;">{
    padding: 0<span style="color: #000000;">;
    margin: 0<span style="color: #000000;">;
}
.<span style="color: #000000;">logsBox{
    margin:<span style="color: #000000;">5px;
    padding:<span style="color: #000000;"> 5px;
    width:<span style="color: #000000;"> 600px;
    background: <span style="color: #008000;">#<span style="color: #008000;">000;
    color:<span style="color: #008000;">#<span style="color: #008000;">fff;
    font-size:<span style="color: #000000;"> 13px;
    <span style="color: #0000ff;">float:<span style="color: #000000;"> left;
}
.logsBox .<span style="color: #000000;">line{
    margin: 12px 0<span style="color: #000000;">;
}
</style>
<div style="color: #0000ff;">class="logsBox apache">
    <div style="color: #0000ff;">class="line">日志读取...</div>
</div>
<div style="color: #0000ff;">class="logsBox nginx">
    <div style="color: #0000ff;">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"><span style="color: #000000;">
    $(<span style="color: #0000ff;">function<span style="color: #000000;">(){

        <span style="color: #0000ff;">function showLogs(api,<span style="color: #000000;">showClass){
            <span style="color: #0000ff;">function<span style="color: #000000;"> readLogs(){
                $.<span style="color: #000000;">ajax({
                    url:api,<span style="color: #000000;">
                    type:"get",<span style="color: #000000;">
                    dataType:"text",<span style="color: #000000;">
                    success:<span style="color: #0000ff;">function<span style="color: #000000;">(data){
                        $(showClass).<span style="color: #000000;">html(data);
                    }
                });
            }
            readLogs();
            setInterval(readLogs,5000<span style="color: #000000;">);
        }
        showLogs("?action=showNginxLogs",".nginx"<span style="color: #000000;">);
        showLogs("?action=showApacheLogs",".apache"<span style="color: #000000;">);
    });
</script>
</div>
<p> </p>
</div>
<div id="MySignature"></div>
<div class="clear"></div>
</div>
</div>
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板