27

I can't understand why I have to use chmod to get the correct permissions.. The file is created succesfully but with 0755 and not 0775 that I specify in mkdir .

( http://php.net/manual/en/function.mkdir.php )

I have to do chmod after mkdir to set the correct permissions.

Safe mode is off in php.ini and the folder belongs to php's group and owner (www-data)

This doesn't work:

  if(!is_dir("/var/www/customers/$username/$project_name")) 
  {
    mkdir("/var/www/customers/$username/$project_name",0775);

  }

But this does:

  if(!is_dir("/var/www/customers/$username/$project_name")) 
  {
    mkdir("/var/www/customers/$username/$project_name");
    chmod("/var/www/customers/$username/$project_name",0775);

  }
1
  • it's allthumbs php developers. because bash mkdir foo -m=777 works perfect Feb 18 at 11:11

3 Answers 3

44

Yes, it's because of umask...

from comments of docs: http://php.net/manual/en/function.mkdir.php

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

mkdir($dir, 0777);

The created folder actually has permissions of 0755, instead of the specified 0777. Why is this you ask? Because of umask(): http://php.net/manual/en/function.umask.php

The default value of umask, at least on my setup, is 18. Which is 22 octal, or 0022. This means that when you use mkdir() to CHMOD the created folder to 0777, PHP takes 0777 and substracts the current value of umask, in our case 0022, so the result is 0755 - which is not what you wanted, probably.

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

$old_umask = umask(0);

Right before creating a folder with mkdir() to have the actual value you put be used as the CHMOD. If you would like to return umask to its original value when you're done, use this:

umask($old_umask);

3
  • 3
    so chmod is not affected by umask? Jun 3, 2011 at 15:55
  • nope... only when you create folders/files you are affected by umask.
    – confiq
    Jun 3, 2011 at 15:59
  • 1
    from real docs -- permissions The permissions are 0777 by default, which means that the func don't care about this arg and will ignore it, as same as in windows :P good luck Feb 18 at 11:16
1

I think you may have to modify your umask.

As noted on the mkdir manpage:

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

Now, looking at the umask() manpage, one of the comment listed confirms my inner thought:

"It is better to change the file permissions with chmod() after creating the file."

In other words, I believe the way you are doing it is more secure:

Set your umask so that files are created private to your user, then use chmod to open them up.

2
  • To clarify, on a shared server, it is absolutely not more secure to chmod after creation. Someone could hide a tiny script to receive notifications for newly created files with common names like LocalSettings, and grab a file handle the moment it's created; he can then read it even after it's chmodded and you add your password data there. It's not relevant for most hosting setups, but out of habit on UNIX you should always make files with the permissions you intend them to have. Jun 3, 2011 at 16:02
  • @Nicholas I have noticed there is contradictary information on the subject of file creation permissions. In my opinion it is reasonable or safe to chmod() a file after creation when you want to grant extra permissions instead of removing them. For example, it is completely safe to set the umask to 0077 and then chmoding the files you create afterward. Creating a file and removing permissions afterwards is the scenario where there is a (very minimal) security risk. I think the chances for an attacker to get a filehandle on a newly created file between 2 function calls are very, very low. Jun 3, 2011 at 16:49
0

Try calling this function before you make your directory: clearstatcache(); Also, maybe you should check if you can do it using just mkdir if you siwtch to another user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.