mkdir ignore mode, but use recursive parameter
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 directo开发者_如何学JAVAries one by one?
Why don't you set the mode to the default value 0777.
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.
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.
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);
精彩评论