PHP chmod 失败?

标签 php unix ubuntu file-permissions chmod

我正在运行 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)
)

为什么 ./I/do/not/exist/ 的(子)文件夹都没有获得指定的(0777)权限?

最佳答案

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

关于PHP chmod 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6839786/

相关文章:

PHP Laravel : No connection could be made because the target machine actively refused it

php - CSS div 的高度不对齐

javascript - 日期处理 Unix 日期不正确(或者我错误地使用日期?)

PHP - 从 html 上的关联数组过滤和输出数据

javascript - Yii 为 JavaScript 编码数组

linux - 通过具有基本身份验证的代理在curl中发送请求

ubuntu - Nifi 连接 Elasticsearch 时出现错误

php - 强制 PHP CLI 将文件写入 www-data

linux - Unix - 递归计算文件类型的数量

unix - 如何在命令行上按名称执行程序而无需输入当前目录?