全国服务热线:0551-64931480

11
20-02

thinkphp5日志文件夹及文件权限问题的解决

云库科技 724104 0

由于www用户和root用户(比如cmd的cli进程日志)都有可能对log文件夹或文件进行创建和读写。
如果是先由www用户创建的log文件夹活文件,则不会出任何问题。但是如果是先由root用户创建的文件,然后再由www用户角色去读写就会出现异常报错。
因为一般默认创建的log文件的权限是  -rw-r--r-,也就是www没有权限去写入root用户创建的log文件。
网上的方法大体就是像下面代码一样在mkdir的时候修改目录的权限

编辑脚本文件//thinkphp/library/think/log/driver/File.php
$destination = $this->getMasterLogFile();
 
$path = dirname($destination);
if (PHP_SAPI != 'cli') {
     !is_dir($path) && mkdir($path, 0755, true);
}else{
     !is_dir($path) && mkdir($path, 0777, true) && chmod($path, 0777);
}

但是上面只能修改文件夹的权限,并没有修改文件夹下具体的.log文件的权限。
【解决办法】:
修改文件:\thinkphp\library\think\log\driver\File.php里的write()函数

protected function write($message, $destination, $apart = false, $append = false)
    {
        ...
        if (PHP_SAPI == 'cli') {
            $message = $this->parseCliLog($info);
        } else {
            // 添加调试日志
            $this->getDebugLog($info, $append, $apart);
 
            $message = $this->parseLog($info);
        }
 
        //return error_log($message, 3, $destination);
 
        /** By Cockor.com Start */
        if (!is_file($destination)) {
            $first = true;
        }
 
        $ret = error_log($message, 3, $destination);
 
        try {
            if (isset($first) && is_file($destination)) {
                chmod($destination, 0777);
                unset($first);
            }
        } catch (\Exception $e) { }
        return $ret;
        /** By Cockor.com End */
    }


评论列表(0)
暂无评论