由于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 */
}
發表評論 取消回復