[PHP] 读取大文件并显示
2023-02-18 15:47:24 时间
使用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['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>
相关文章
- 谣言检测(ClaHi-GAT)《Rumor Detection on Twitter with Claim-Guided Hierarchical Graph Attention Networks》
- 华为音频编辑服务,实时分离人声、伴奏和乐器声
- 谣言检测(PSIN)——《Divide-and-Conquer: Post-User Interaction Network for Fake News Detection on Social Media》
- 谣言检测(PLAN)——《Interpretable Rumor Detection in Microblogs by Attending to User Interactions》
- 谣言检测()——《Debunking Rumors on Twitter with Tree Transformer》
- 谣言检测(GACL)《Rumor Detection on Social Media with Graph Adversarial Contrastive Learning》
- 鸿蒙手表定位功能Demo体验,适用儿童、老年和外出旅游安全市场
- 谣言检测——(PSA)《Probing Spurious Correlations in Popular Event-Based Rumor Detection Benchmarks》
- 春天健身正当时,送你一份人体健康检测指南
- 谣言检测——(GCAN)《GCAN: Graph-aware Co-Attention Networks for Explainable Fake News Detection on Social Media》
- 分析服务:以数据量化渠道投放成本与转化效果
- 论文解读(RvNN)《Rumor Detection on Twitter with Tree-structured Recursive Neural Networks》
- 谣言检测——(EBGCN)《Towards Propagation Uncertainty: Edge-enhanced Bayesian Graph Convolutional Networks for Rumor Detection》
- 谣言检测——(Bi-GCN)《Rumor Detection on Social Media with Bi-Directional Graph ConvolutionalNetworks》
- 谣言检测——(GLAN)《Jointly embedding the local and global relations of heterogeneous graph for rumor detection》
- 谣言检测——《MFAN: Multi-modal Feature-enhanced Attention Networks for Rumor Detection》
- 谣言检测——《社会网络谣言检测综述》
- 论文解读(PairNorm)《PairNorm: Tackling Oversmoothing in GNNs》
- 论文解读(NWR)《Graph Auto-Encoder via Neighborhood Wasserstein Reconstruction》
- 论文解读(GATv2)《How Attentive are Graph Attention Networks?》