Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in PHP if you use Mkdir recursive flag do the nest directories not chmod?

Tags:

php

chmod

mkdir

I am using mkdir to create normally 2 nested directories for a file structure. The directories it creates are always set to 0755. The code I am using however is.

 mkdir('path_one/path_two', 0777, true);

I have tried then doing

 chmod('path_one/path_two', 0777);

but that only sets the final directory as 0777. What would cause mkdir not to function properly?

like image 865
Case Avatar asked Sep 07 '13 21:09

Case


2 Answers

mkdir is functioning correctly. The intermediate directories created are set based on the current umask. You want something like:

umask(0777);
mkdir('path_one/path_two', 0777, true);
like image 185
Tim Duncklee Avatar answered Sep 21 '22 11:09

Tim Duncklee


From the php manual:

The mode is also modified by the current umask, which you can change using umask().

Note that any bits that are set in umask() are unset in the result that's used by mkdir(). The default umask is 0022 and the default create mode for mkdir is 0777, which gives a result value of 0755. This applies for all created directories.

like image 42
Arjan Avatar answered Sep 18 '22 11:09

Arjan