thinkphp5日志文件權限的問題


由於www用戶和root用戶(比如command的cli進程日志)都有可能對log文件進行讀寫。

如果是由www用戶創建的log文件,不會出任何問題。

但是如果是先由root用戶創建的log文件,然后再到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);
 
        /** 解決root生成的文件,www用戶沒有寫權限的問題 by Werben 20190704 begin */
        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;
        /** 解決root生成的文件,www用戶沒有寫權限的問題 by Werben 20190704 end */
        ...
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM