views:

76

answers:

4

PHP's mkdir function has the following signature:

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )

I would like to call this function specifying the $pathname and $recursive set to true, but I wouldn't like to specify the $mode parameter. We control permissions on new files/directories on the server level and as such don't wish to specify them in my code.

So what should I pass as $mode so that it is effectively ignored and no chmod-ing takes place? I haven't found a way.

Maybe the only solution is to write a custom mkdir function that will break the path and create directories one by one?

+3  A: 

Why don't you set the mode to the default value 0777.

dejavu
This should be a comment.
Pekka
I don't wish to have my files/directories set to 0777
Jan Hančič
oh, never mind, this seems to use the default permission set by the server. Forgot about `umask`. Cheers!
Jan Hančič
A: 

Sorry there is no way to escape $mode param if you want to give $recursive param.

You can store mode in a variable and use it any where later-

$mode = 0777;

mkdir($file_name, $mode, $recursive);
Sadat
+1  A: 

0777?

Unless I'm missing something, the signature shows three optional parameters. The first two, $mode and $recursive have a default value of 0777 and false, respectively, when not specified. Specifying it explicitly shouldn't change the behavior.

lc
I guess two coffees are not enough for me to wake up today :) To bad I can't accept 3 answers. Cheers!
Jan Hančič
+1  A: 

0777 is the default. If you do not specify the parameter, 0777 will be used instead so any attempt to "get around" specifying it is moot.

I don't think you will get around this at all because this is not a behaviour by PHP: GNU/Linux's mkdir() does the same thing:

The parameter mode specifies the permissions to use. It is modified by the process's umask in the usual way: the permissions of the created directory are (mode & ~umask & 0777).

I'm not sure whether this is something you need to worry about at all. If it is, I think the best thing you can do is check out the permissions of the parent directory and apply that.

Pekka
I guess two coffees are not enough for me to wake up today :) To bad I can't accept 3 answers. Cheers!
Jan Hančič