使用php编写在线浏览日志文件的小应用
由于公司内部开发文件权限限制,非主管无法随意改动获取正式服文件,所以写了一个这样的小应用来查看日志文件
php文件
abstract class getDir
{
protected $dir = null;
protected $chmod = 0;
protected $file = null;
public function __construct($dir = null, $type = 0)
{
if (!$type) {
if (empty($dir)) {
$this->dir = dirname(__FILE__);
} else {
$this->dir = $dir;
}
} else {
$this->dir = dirname(__FILE__);
$this->file = dirname(__FILE__) . "/" . $dir;
}
$this->chmod = $this->getChmod($this->dir);
}
// 获取权限
protected function getChmod($filepath)
{
return substr(base_convert(@fileperms($filepath), 10, 8), -4);
}
public function get_chmod()
{
return $this->chmod;
}
public function get_dir()
{
return $this->dir;
}
abstract function readfile();
abstract function printFiles();
}
class getDirectory extends getDir
{
public function printFiles()
{
$files = scandir($this->dir);
echo <<< EOD
dir:{$this->dir}<br>
EOD;
echo <<< EOD
chmod:{$this->chmod}<br>
EOD;
foreach ($files as $item) {
$d = mb_substr($item,0,1);
if ($d == "."){
continue;
}
echo <<< EOD
<a href="?file=$item">$item</a><br>
EOD;
}
}
public function readfile()
{
if (is_file($this->file)) {
$file_type = mb_substr($this->file, strrpos($this->file, ".") + 1);
if ($file_type!="log"){
exit(sprintf("The file type is %s not log file!",$file_type));
}
if (!is_writable($this->file)){
echo "The file can"t read!";
}
$chmod = $this->getChmod($this->file);
echo <<< EOD
dir:{$this->dir}<br>
EOD;
echo <<< EOD
chmod:$chmod<br>
EOD;
$str = file_get_contents($this->file);
$str = str_replace("
","<br />",$str);
echo $str;
}else{
echo "This is not a file!";
}
}
}
使用
if (isset($_REQUEST["file"])) {
$files = $_REQUEST["file"];
$gd = new getDirectory($files, 1);
$gd->readfile();
} else {
$gd = new getDirectory();
$gd->printFiles();
}
日志目录(例子)
日志内容
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: php readline扩展安装
- 下一篇: JNI学习之步步深入一