使用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("\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();                
}                                     
日志目录(例子)

日志目录

日志内容

日志内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex-Leung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值