PHP 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)
)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在创建目录之前,您可能必须先清除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.
看起来您有 umask 022。尝试在
mkdir
之前添加umask(0)
It looks like you have umask 022. Try to add
umask(0)
beforemkdir
只需阅读手册:
检查您的系统 umask 并相应地使用模式参数。
或者将 umask 设置为您能够处理的值。
或者在chmod'ding之后进行处理目录的创建。
Just read the manual:
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.