开发者

Permissions with mkdir won't work

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/c开发者_如何学Customers/$username/$project_name");
    chmod("/var/www/customers/$username/$project_name",0775);

  }


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);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜