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

2 篇文章 0 订阅

场景:在服务器上添加了一个定时删除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);

修改为

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

修改为

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;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值