PHP chmod 失败?

发布于 2024-11-26 12:59:12 字数 1660 浏览 2 评论 0原文

我正在运行 PHP 5.3.5-1ubuntu7.2(使用 safe_mode = Off),并且无法正确设置某个文件或目录中的模式。 PHP 脚本,我编写了以下测试(只是为了确保):

$result = array();

if (mkdir('./I/do/not/exist/', 0777, true) === true)
{
    $result['./I/'] = sprintf('%s (%s)', getFileOwner('./I/'), getFilePermissions('./I/'));
    $result['./I/do/'] = sprintf('%s (%s)', getFileOwner('./I/do/'), getFilePermissions('./I/do/'));
    $result['./I/do/not/'] = sprintf('%s (%s)', getFileOwner('./I/do/not/'), getFilePermissions('./I/do/not/'));
    $result['./I/do/not/exist/'] = sprintf('%s (%s)', getFileOwner('./I/do/not/exist/'), getFilePermissions('./I/do/not/exist/'));
    $result[__DIR__] = sprintf('%s (%s)', getFileOwner(__DIR__), getFilePermissions(__DIR__));
    $result[__FILE__] = sprintf('%s (%s)', getFileOwner(__FILE__), getFilePermissions(__FILE__));
}

echo '<pre>';
print_r($result);
echo '</pre>';

function getFileOwner($path)
{
    $user = posix_getpwuid(fileowner($path));
    $group = posix_getgrgid(filegroup($path));

    return implode(':', array($user['name'], $group['name']));
}

function getFilePermissions($path)
{
    return substr(sprintf('%o', fileperms($path)), -4);
}

这是输出:

Array
(
    [./I/] => www-data:www-data (0755)
    [./I/do/] => www-data:www-data (0755)
    [./I/do/not/] => www-data:www-data (0755)
    [./I/do/not/exist/] => www-data:www-data (0755)
    [/home/alix/Server/_] => alix:alix (0777)
    [/home/alix/Server/_/chmod.php] => alix:alix (0644)
)

Why do none of the (sub-)folders of ./I/do/not/exist/ get指定的(0777) 权限?

I'm running PHP 5.3.5-1ubuntu7.2 (with safe_mode = Off) and I'm unable to correctly set the mode for any file or directory from within a PHP script, I coded the following test (just to make sure):

$result = array();

if (mkdir('./I/do/not/exist/', 0777, true) === true)
{
    $result['./I/'] = sprintf('%s (%s)', getFileOwner('./I/'), getFilePermissions('./I/'));
    $result['./I/do/'] = sprintf('%s (%s)', getFileOwner('./I/do/'), getFilePermissions('./I/do/'));
    $result['./I/do/not/'] = sprintf('%s (%s)', getFileOwner('./I/do/not/'), getFilePermissions('./I/do/not/'));
    $result['./I/do/not/exist/'] = sprintf('%s (%s)', getFileOwner('./I/do/not/exist/'), getFilePermissions('./I/do/not/exist/'));
    $result[__DIR__] = sprintf('%s (%s)', getFileOwner(__DIR__), getFilePermissions(__DIR__));
    $result[__FILE__] = sprintf('%s (%s)', getFileOwner(__FILE__), getFilePermissions(__FILE__));
}

echo '<pre>';
print_r($result);
echo '</pre>';

function getFileOwner($path)
{
    $user = posix_getpwuid(fileowner($path));
    $group = posix_getgrgid(filegroup($path));

    return implode(':', array($user['name'], $group['name']));
}

function getFilePermissions($path)
{
    return substr(sprintf('%o', fileperms($path)), -4);
}

And this is the output:

Array
(
    [./I/] => www-data:www-data (0755)
    [./I/do/] => www-data:www-data (0755)
    [./I/do/not/] => www-data:www-data (0755)
    [./I/do/not/exist/] => www-data:www-data (0755)
    [/home/alix/Server/_] => alix:alix (0777)
    [/home/alix/Server/_/chmod.php] => alix:alix (0644)
)

Why do none of the (sub-)folders of ./I/do/not/exist/ get the specified (0777) permissions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里人 2024-12-03 12:59:12

在创建目录之前,您可能必须先清除umask。不过,建议使用 chmod 来调整权限,而不是依赖 umask。

You may have to clear the umask first before creating the directory. However it is recommended to adjust the permissions using chmod instead of relying on umask.

清风无影 2024-12-03 12:59:12

看起来您有 umask 022。尝试在 mkdir 之前添加 umask(0)

It looks like you have umask 022. Try to add umask(0) before mkdir

面如桃花 2024-12-03 12:59:12

只需阅读手册:

该模式也会根据当前的 umask 进行修改,您可以使用 umask 进行更改()

检查您的系统 umask 并相应地使用模式参数。

或者将 umask 设置为您能够处理的值。

或者在chmod'ding之后进行处理目录的创建。

Just read the manual:

The mode is also modified by the current umask, which you can change using umask().

Check your systems umask and make use of the mode parameter accordingly.

Alternatively set the umask to a value you're able to deal with.

Or take care of chmod'ding after the creation of the directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文