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?
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.