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