4

I have the following code:

function makedirs($dirpath, $mode = 0775, $recursive = true) {
    return is_dir($dirpath) || mkdir($dirpath, $mode, $recursive);
}

$dir = 'path/to/folder/';

makedirs($dir, 0775);

The problem is: even when passing 0775 or anything else as parameter for $mode, mkdir() creates 0755 permition folders.

For exemple the previows code will return:

  • path/ (0755)
  • to/ (0755)
  • folder/ (0755)
Machado
  • 8,965
  • 6
  • 43
  • 46
  • Check the umask value. you could type in umask in the shell to find out the mask value. I'm guessing your umask value to be 022 – user3425867 Apr 20 '16 at 14:43
  • `$dir = 'path/to/folder/' makedirs($dir, 0775)` that's incomplete if that's what you're really using. If that's pseudo-code, please post proper syntax. – Funk Forty Niner Apr 20 '16 at 14:49
  • `$mode = 0775` the leading zero is probably being treated as an octal btw because of the custom function. Try quoting it and same for inside `makedirs($dir, 0775)`. – Funk Forty Niner Apr 20 '16 at 14:51
  • what does `var_dump();` reveal? – Funk Forty Niner Apr 20 '16 at 14:56
  • @Fred-ii- the folder I'm using doesn't matter, the function is successful creating the folder and writing some files in it. The question is the permitions. (Note that you probably want to specify the mode as an octal number, which means it should have a leading zero. - http://php.net/manual/en/function.mkdir.php) – Machado Apr 20 '16 at 14:56
  • Might be relevant: http://stackoverflow.com/questions/25104990/mkdir-always-sets-folders-to-read-only/25105031#25105031 – Charlotte Dunois Apr 20 '16 at 14:57
  • @Fred-ii- var_dump(makedirs($dir, 0775)) results bool true. – Machado Apr 20 '16 at 14:58
  • you have an answer below to consult then. Oh and I thought that in using a custom function would have been an issue, that's why I said that @Machado – Funk Forty Niner Apr 20 '16 at 14:59
  • Okay, me too, but due to the octal number sintax, thank you anyway @Fred-ii- – Machado Apr 20 '16 at 15:10
  • *You're welcome* @Machado glad to see it worked out, *cheers* – Funk Forty Niner Apr 20 '16 at 15:12

1 Answers1

3

You could do the following

function makedirs($dirpath, $mode = 0775, $recursive = true) {
    $oldMask=umask(002);
    $status = is_dir($dirpath) || mkdir($dirpath, $mode, $recursive);
    umask($oldMask);
    return $status;
}

$dir = 'path/to/folder/'
makedirs($dir, 0775);

Note : Although you could use umask(0) to allow even 777 permissions It's not recommended as it could pose security issues.

Edit

Try setting the umask value system wide for all users or for yourself to remove the umask code from php. Although the above code would work, setting umask in php scripts is not recommended.

According to the PHP Manual Page

Avoid using this function in multithreaded webservers. It is better to change the file permissions with chmod() after creating the file. Using umask() can lead to unexpected behavior of concurrently running scripts and the webserver itself because they all use the same umask.

You can setup umask in /etc/bashrc or /etc/profile file for all users. By default most Linux distro set it to 0022 (022) or 0002 (002). Open /etc/profile or ~/.bashrc file, enter:

# vi /etc/profile

OR

$ vi ~/.bashrc

Append/modify following line to setup a new umask: umask 022

Save and close the file. Changes will take effect after next login. All UNIX users can override the system umask defaults in their /etc/profile file, ~/.profile (Korn / Bourne shell) ~/.cshrc file (C shells), ~/.bash_profile (Bash shell) or ~/.login file (defines the user’s environment at login).

Source http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html

user3425867
  • 644
  • 6
  • 14
  • your comment about `umask` brought me here: http://php.net/manual/en/function.umask.php, where the first example explains your answer and can be usefull to others. – Machado Apr 20 '16 at 15:03
  • 1
    It does make more sense to use chmod. You may still have to set the umask value of your system to 002 atleast once. You can setup umask in `/etc/bashrc` or `/etc/profile` file for all users. – user3425867 Apr 20 '16 at 15:07
  • Well, now I'm using chmod() after mkdir() to give the folder the desired permitions for security reasons. – Machado Apr 20 '16 at 18:53