这里我以mysql查询日志为源文件输入,直接显示到网页中查看。
<?php $file = 'mysqld-slow.log'; $fp = fopen($file, "r"); $line = 5000; $pos = -2; $t = " "; $data = " "; while ($line > 0) { while ($t != "\n") { $flag = fseek($fp, $pos, SEEK_END); if (fseek($fp, $pos, SEEK_END) == -1) { rewind($fp); $t = "\n"; $line = 0; } else { $t = fgetc($fp); $pos--; } } $t = " "; $line--; } while(!feof($fp)){ $data .= fgets($fp); $data .= '<br />'; } fclose($fp); echo $data ?>
<?php $file = '/path/to/file'; $fp = fopen($file, "r"); $line = 2000; $pos = -2; $t = " "; $data = " "; while ($line > 0) { while ($t != "\n") { $flag = fseek($fp, $pos, SEEK_END); if (fseek($fp, $pos, SEEK_END) == -1) { rewind($fp); $t = "\n"; $line = 0; } else { $t = fgetc($fp); $pos--; } } $t = " "; $line--; } while(!feof($fp)){ $data .= fgets($fp); $data .= '<br />'; } fclose($fp); echo $data ?>