Heim > php教程 > php手册 > [PHP] Große Dateien lesen und anzeigen

[PHP] Große Dateien lesen und anzeigen

WBOY
Freigeben: 2016-08-04 08:53:18
Original
1206 Leute haben es durchsucht

使用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>
Nach dem Login kopieren

 

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage