tp5 解決root生成的文件,www用戶沒有寫權限的問題


場景:在服務器上添加了一個定時刪除cache緩存文件的任務,由於在執行之后會在runtime中生成一個文件,如果正好是月初一號就會創建這個月份的文件夾,由於這個自動任務是root用戶執行,運行項目寫日志是www用戶,所以當項目運行再寫入日志時會沒有權限。

出現報錯

PHP Fatal error: Uncaught exception ‘think\exception\ErrorException’ with message ‘error_log(D:\web\xinluchuntian.com\minishop\runtime\log\201702\11.log): failed to open stream: Permission denied’ in D:\web\xinluchuntian.com\minishop\core\library\think\log\driver\File.php:98
Stack trace:
#0 [internal function]: think\Error::appError(2, ‘error_log(D:\we…’, ‘D:\web\xinluchu…’, 98, Array)
#1 D:\web\xinluchuntian.com\minishop\core\library\think\log\driver\File.php(98): error_log(’[ 2017-02-11T17…’, 3, ‘D:\web\xinluchu…’)
#2 D:\web\xinluchuntian.com\minishop\core\library\think\Log.php(157): think\log\driver\File->save(Array)
#3 D:\web\xinluchuntian.com\minishop\core\library\think\Error.php(84): think\Log::save()
#4 [internal function]: think\Error::appShutdown()
#5 {main}
thrown in D:\web\xinluchuntian.com\minishop\core\library\think\log\driver\File.php on line 98

解決辦法,需要修改兩個位置,首先按找到thinkphp/library/log/driver/file.php

當前tp5版本:5.0.15
1. 找到56行(不同tp版本可能會不一樣,save方法中)
!is_dir($path) && mkdir($path, 0755, true);
1
修改為

!is_dir($path) && mkdir($path, 0755, true) && chmod($path,0777);
1
2.找到128行(不同tp版本可能會不一樣,write方法中)
return error_log($message, 3, $destination);
1
修改為

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;
 

 


免責聲明!

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



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