PHP mkdir vs chmod

Asked

Viewed 318 times

4

I was creating folders dynamically by PHP, when I came across the following situation.

Occurred

Create a folder tmp with permission 777 in /www

mkdir('/www/tmp', intval('0777',8), true);

Turns out he’s not allowed 777, but 755;

drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 .
drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 ..
drwxr-xr-x 2 www-data www-data 4096 Jun  8 17:51 tmp

Solution

I added the command chmod to be executed shortly thereafter

mkdir('/www/tmp', intval('0777',8), true);
chmod('/www/tmp', intval('0777',8));

And so it was with the desired permission 777.

drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 .
drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 ..
drwxrwxrwx 2 www-data www-data 4096 Jun  8 17:51 tmp

Doubt

  • Are there any settings in PHP that limit the permission a command can give? Ex.: mkdir could only use 444.
  • This was some PHP error?

Addendum

PHP is based on Shell. In Shell if I do :

mkdir /www/tmp;

drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 .
drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 ..
drwxr-xr-x 2 www-data www-data 4096 Jun  8 17:51 tmp

chmod 0777 /www/tmp;

drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 .
drwxrwxrwx 3 www-data www-data 4096 Jun  8 17:51 ..
drwxrwxrwx 2 www-data www-data 4096 Jun  8 17:51 tmp

In other words, PHP could only be performing mkdir? ignoring the second parameter intval('0777',8)?

  • Remove the third parameter defined as true and test again, only mkdir(). intval() also unnecessary. Put 0777 without quotation marks or leave empty because the default is 0777.

  • Third parameter is recursive, therefore permission is not relevant.

  • @Danielomine the 2nd parameter should be octal based, if I convert a string for octal or directly for an octal, would be the same thing. As for the 3rd parameter is to leave the sub-folders (future) with the same permission. Even so I tested as you commented without 3rd and directly 0777 the result was the same. :/

2 answers

8


It seems to be because of the umask.

Here is the translation of the answer https://stackoverflow.com/questions/6229353/permissions-with-mkdir-wont-work

You may notice that when you create a new directory using this code:

mkdir($dir, 0777);

The created folder actually has 0755 permissions instead of 0777. Why is that? Because of umask(): http://www.php.net/umask

The default value of umask, at least in my configuration, is 18. Which is 22 octal or 0022. That means when you use mkdir() with 777 permissions, PHP takes 0777 and subtracts the current value of umask. In our case 0022, then the result is 0755 - which is not what you wanted, probably.

The "fix" for this is simple, include this line:

$old_umask = umask(0);

Just before creating a folder with mkdir() so that the actual value you place is used as the CHMOD. If you want to return umask to its original value when finished, use this:

umask($old_umask);
  • I’ll look into this case and I’ll get back to you, I’ll give you my +1 for the info

  • 4

    In the documentation of umask has an important note that says, in free translation: "Avoid using this function in case where the server is multithreaded. It is better to change the permission via chmod after creating the file. Unexpected behaviors can occur with competing scripts and with the webserver because they all use the same umask"

4

The mode is modified by its current user mask (umask), that is 022 in this case

You inform the initial permission at mkdir but the information of umask to obtain the royal permission :

  0777
- 0022
======
  0755 = rwxr-xr-x.

If you don’t want this to happen, you need to set up your umask temporarily to zero, so it has no effect anymore. You can do this with the following excerpt.

$oldmask = umask(0);
mkdir("test", 0777);
umask($oldmask);

The first line changes umask to zero while storing the previous $oldmask. The second line makes the directory using the desired permissions. The third line restores the umask for the original value.

Source

Documentation: See the PHP documentation for umask and mkdir for more details

  • Yours is simpler and easier to understand, +1, but @Diegoschmidt had already given the answer, thanks for the collaboration.

  • Yes, I only saw it because of your comment. But I’ll leave it because of the same readability. I hope to have helped you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.