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

时间:2021-09-01
本文章向大家介绍tp5 解决root生成的文件,www用户没有写权限的问题,主要包括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;
 

原文地址:https://www.cnblogs.com/xiaogou/p/15213372.html