Php 警告:chmod():不允许操作

问题描述 投票:0回答:1

我的 php 函数 chmod 出现错误 https://www.php.net/manual/en/function.chmod.php 有什么解决方案吗? 我正在运行具有 root 访问权限的 Linux Debian 13.05 和 PHP 8.2。 Php 警告:chmod():文件中没有这样的文件或目录:index.php 在线:8 添加空文件后,我们得到: PHP 警告:chmod():文件中不允许操作:index.php 在线:8 Php 警告:fopen(./access.log):无法打开流:文件中的权限被拒绝:index.php 在线:9

$root_path = './'; define('LOG_FILE', $root_path . 'access.log');

function add_log_entry($access = '')
{
    if (LOG_FILE)
    {
        chmod(LOG_FILE, 0755);
        $fopen = fopen(LOG_FILE, 'ab');
    }
}   
php chmod
1个回答
0
投票

如果目录和文件是由同一用户和同一用户组创建的,并且可以继续,直到我们不需要使用“sudo chmod”或 ftp,则这是有效的。 Linux 上的另一个解决方案是使用我们自己的主/用户目录中的子目录,这可以简化。

$root_path = './'; 
define('LOG_FILE', $root_path . 'access.log');
    
    function add_log_entry($access = '')
    {
        if (LOG_FILE)
        {
            // Default to write
            $perms = 2;
            
            // Add read/write and execute bit to owner among perms
            $owner_perm = (4 | 2) | (1);
            $file_perm  = ($owner_perm << 6) + ($perms << 3) + ($perms << 0);

            // Compute directory permissions, 7 is for all
            $perm = ($perms !== 0) ? ($perms | 1) : $perms;
            $dir_perm = (($owner_perm | 1) << 6) + ($perm << 3) + ($perm << 0);
    
            // Don't chmod links as mostly those require 0777 and that cannot be changed
            if (is_dir(LOG_FILE) || (is_link(LOG_FILE)))
            {
                if (true !== @chmod(LOG_FILE, $dir_perm))
                {
                    print('Filesystem_cannot_change_access_log_file_permissions.');
                }
            }
            else if (is_file(LOG_FILE))
            {
                if (true !== @chmod(LOG_FILE, $file_perm))
                {
                    print('Filesystem_cannot_change_access_log_file_permissions.');
                }
            }
            
            $fopen = @fopen(LOG_FILE, 'ab'); //add more code
        }
    }

 
© www.soinside.com 2019 - 2024. All rights reserved.