问题
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?
回答1:
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.
回答2:
It looks like you have umask 022. Try to add umask(0)
before mkdir
回答3:
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.
来源:https://stackoverflow.com/questions/6839786/php-fails-to-chmod