zoukankan      html  css  js  c++  java
  • thinkphp5日志文件权限的问题

    由于www用户和root用户(比如command的cli进程日志)都有可能对log文件进行读写。

    如果是由www用户创建的log文件,不会出任何问题。

    但是如果是先由root用户创建的log文件,然后再到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文件的权限。

    【解决办法】:

    修改文件: hinkphplibrary hinklogdriverFile.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);
     
            /** 解决root生成的文件,www用户没有写权限的问题 by Werben 20190704 begin */
            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;
            /** 解决root生成的文件,www用户没有写权限的问题 by Werben 20190704 end */
            ...
        }
  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/werben/p/11550320.html
Copyright © 2011-2022 走看看