使用php编写在线浏览日志文件的小应用

来源:互联网 发布:什么是世界货币 知乎 编辑:程序博客网 时间:2024/05/23 01:26

由于公司内部开发文件权限限制,非主管无法随意改动获取正式服文件,所以写了一个这样的小应用来查看日志文件

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 <<< EODdir:{$this->dir}<br>EOD;        echo <<< EODchmod:{$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("\r\n","<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();                }                                     

日志目录(例子)

日志目录

日志内容

日志内容

0 0