thinkphp5.1日志文件夹及文件权限问题 队列生成文件权限异常

thinkphp5.1日志文件夹及文件权限问题,由于www用户和root用户(比如cmd的cli进程日志)都有可能对log文件夹或文件进行创建和读写。如果是先由www用户创建的log文件夹活文件,则不会出任何问题。但是如果是先由root用户创建的文件,然后再由www用户角色去读写就会出现异常报错。因为一般默认创建的log文件的权限是  -rw-r--r-,也就是www没有权限去写入root用户创建的log文件。

【解决办法】:

修改文件:\thinkphp\library\think\log\driver\File.php

/**

* 日志写入接口

* @access public

* @param  array    $log    日志信息

* @param  bool     $append 是否追加请求信息

* @return bool

*/

public function save(array $log = [], $append = false)

{

$destination = $this->getMasterLogFile();

$path = dirname($destination);

!is_dir($path) && mkdir($path, 0777, true); //修改为0777

  chmod($path, 0777); //设置目录为0777

 // 如果上面代码不起作用则使用下面的代码

        $path = dirname($destination);

        $oldmask = umask(0);

        if(!is_dir($path)){

            $oldmask = umask(0);

            mkdir($path, 0777, true);

            umask($oldmask);

        }

        // !is_dir($path) && mkdir($path, 0777, true);

        // chmod($path, 0777);

        // end 如果上面代码不起作用则使用下面的代码

$info = [];

foreach ($log as $type => $val) {

foreach ($val as $msg) {

if (!is_string($msg)) {

$msg = var_export($msg, true);

}

$info[$type][] = $this->config['json'] ? $msg : '[ ' . $type . ' ] ' . $msg;

}

if (!$this->config['json'] && (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level']))) {

// 独立记录的日志级别

$filename = $this->getApartLevelFile($path, $type);

$this->write($info[$type], $filename, true, $append);

unset($info[$type]);

}

}

if ($info) {

return $this->write($info, $destination, false, $append);

}

return true;

}

/**

* 日志写入

* @access protected

* @param  array     $message 日志信息

* @param  string    $destination 日志文件

* @param  bool      $apart 是否独立文件写入

* @param  bool      $append 是否追加请求信息

* @return bool

*/

protected function write($message, $destination, $apart = false, $append = false)

{

// 检测日志文件大小,超过配置大小则备份日志文件重新生成

$this->checkLogSize($destination);

// 日志信息封装

$info['timestamp'] = date($this->config['time_format']);

foreach ($message as $type => $msg) {

$msg = is_array($msg) ? implode(PHP_EOL, $msg) : $msg;

if (PHP_SAPI == 'cli') {

$info['msg']  = $msg;

$info['type'] = $type;

} else {

$info[$type] = $msg;

}

}

if (PHP_SAPI == 'cli') {

$message = $this->parseCliLog($info);

} else {

// 添加调试日志

$this->getDebugLog($info, $append, $apart);

$message = $this->parseLog($info);

}

// return error_log($message, 3, $destination);

/** 解决root生成的文件,www用户没有写权限的问题 */

        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;

        /** End 解决root生成的文件,www用户没有写权限的问题 */

}

PHP mkdir 0777权限问题

 

在linux系统中,即使我们使用root帐号去手工执行php命令

mkdir('test', 0777);

结果文件的权限依然为:

drwxr-xr-x 2 root root   4096 Jun 17 11:28 test

很明显这个权限是 755

为什么会这样呢?

在linux系统中在创建文件/文件夹时有一个默认权限,此权限受 umask 设置影响,在/etc/bashrc配置文件中我们可以找到如下配置:

# By default, we want this to get set.

# Even for non-interactive, non-login shells.

if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then

umask 002

else

umask 022

fi

这里就说明了问题,linux系统中默认的umask为022,与我们的777 &运算之后,就变成了 755,这就是原因所在了。

解决办法:

你可能会想到,我把这里的022 改为 000 不就可以了么?

当然可以,不过这里的设置直接影响到linux系统的默认权限设置,不仅仅是PHP的问题。所以不建议您这么做。

正确的做法应该是:

mkdir('test', 0777);

chmod('test', 0777);

先创建目录,再使用chmod将权限修改为 777 ,这样就达到我们的目的了。

最后,需要注意一点,权限值最好使用八进制表示,即“0”开头,而且一定不要加引号。

本文由 我爱PHP169 作者:admin 发表,其版权均为 我爱PHP169 所有,文章内容系作者个人观点,不代表 我爱PHP169 对观点赞同或支持。如需转载,请注明文章来源。

发表回复