Alix Axel
Alix Axel

Reputation: 154513

PHP fails to chmod?

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?

Upvotes: 3

Views: 1202

Answers (3)

hakre
hakre

Reputation: 197624

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.

Upvotes: 1

lazyhammer
lazyhammer

Reputation: 1228

It looks like you have umask 022. Try to add umask(0) before mkdir

Upvotes: 2

Eelke
Eelke

Reputation: 21993

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.

Upvotes: 3

Related Questions