tp5 solves the problem that the file generated by root, the www user does not have write permission

created at 09-06-2021 views: 1


A task to periodically delete cache files is added to the server. After execution, a file will be generated in runtime. If it happens to be the first day of the month, a folder for this month will be created. Since this automatic task is executed by the root user, The user who runs the project and writes the log is the www user, so when the project runs and then writes to the log, there will be no permission.

error message

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


The solution is to modify two locations, first press to find thinkphp/library/log/driver/file.php

Current tp5 version: 5.0.15
1. Find 56 lines (different tp versions may be different, in the save method)

!is_dir($path) && mkdir($path, 0755, true);

change into

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

2. Find 128 lines (different tp versions may be different, in the write method)

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

change to

if (!is_file($destination)) {
$first = true;

$ret = error_log($message, 3, $destination);
try {
if (isset($first) && is_file($destination)) {
chmod($destination, 0777);
} catch (\Exception $e) {

return $ret;

created at:09-06-2021
edited at: 09-06-2021: