> php教程 > php手册 > [PHP] 대용량 파일 읽기 및 표시

[PHP] 대용량 파일 읽기 및 표시

WBOY
풀어 주다: 2016-08-04 08:53:18
원래의
1215명이 탐색했습니다.

使用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</span>: 0<span style="color: #000000;">;
    margin</span>: 0<span style="color: #000000;">;
}
</span>.<span style="color: #000000;">logsBox{
    margin</span>:<span style="color: #000000;">5px;
    padding</span>:<span style="color: #000000;"> 5px;
    width</span>:<span style="color: #000000;"> 600px;
    background</span>: <span style="color: #008000;">#</span><span style="color: #008000;">000;</span>
    color:<span style="color: #008000;">#</span><span style="color: #008000;">fff;</span>
    font-size:<span style="color: #000000;"> 13px;
    </span><span style="color: #0000ff;">float</span>:<span style="color: #000000;"> left;
}
</span>.logsBox .<span style="color: #000000;">line{
    margin</span>: 12px 0<span style="color: #000000;">;
}
</span></style>
<div <span style="color: #0000ff;">class</span>="logsBox apache">
    <div <span style="color: #0000ff;">class</span>="line">日志读取...</div>
</div>
<div <span style="color: #0000ff;">class</span>="logsBox nginx">
    <div <span style="color: #0000ff;">class</span>="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><span style="color: #0000ff;">function</span><span style="color: #000000;">(){

        </span><span style="color: #0000ff;">function</span> showLogs(api,<span style="color: #000000;">showClass){
            </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> readLogs(){
                $</span>.<span style="color: #000000;">ajax({
                    url</span>:api,<span style="color: #000000;">
                    type</span>:"get",<span style="color: #000000;">
                    dataType</span>:"text",<span style="color: #000000;">
                    success</span>:<span style="color: #0000ff;">function</span><span style="color: #000000;">(data){
                        $(showClass)</span>.<span style="color: #000000;">html(data);
                    }
                });
            }
            readLogs();
            setInterval(readLogs</span>,5000<span style="color: #000000;">);
        }
        showLogs(</span>"?action=showNginxLogs",".nginx"<span style="color: #000000;">);
        showLogs(</span>"?action=showApacheLogs",".apache"<span style="color: #000000;">);
    });
</span></script>
로그인 후 복사

 

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿