Linux mkdir 权限不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6229353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:20:34  来源:igfitidea点击:

Permissions with mkdir won't work

phplinux

提问by Paris Liakos

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

我不明白为什么我必须使用chmod才能获得正确的权限。该文件已成功创建,但使用 0755 而不是 0775 我在mkdir.

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

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

I have to do chmodafter mkdirto set the correct permissions.

我必须chmodmkdir设置正确的权限之后做。

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

php.ini 中的安全模式关闭,该文件夹属于 php 的组和所有者 (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);

  }

采纳答案by confiq

Yes, it's because of umask...

是的,这是因为umask...

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

来自文档的评论: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);

您可能会注意到,当您使用以下代码创建新目录时:

mkdir($dir, 0777);

创建的文件夹实际上有0755的权限,而不是指定的0777。你问为什么?由于 umask(): http://php.net/manual/en/function.umask.php

umask 的默认值,至少在我的设置中,是 18。它是 22 八进制,或 0022。这意味着当您使用 mkdir() 将创建的文件夹 CHMOD 为 0777 时,PHP 取 0777 并减去 umask 的当前值,在我们的例子中是 0022,所以结果是 0755 - 这可能不是你想要的。

对此的“修复”很简单,包括这一行:

$old_umask = umask(0);

就在使用 mkdir() 创建文件夹之前,将您放置的实际值用作 CHMOD。如果您想在完成后将 umask 返回到其原始值,请使用以下命令:

umask($old_umask);

回答by stefgosselin

I think you may have to modify your umask.

我认为您可能需要修改您的 umask。

As noted on the mkdir manpage:

如 mkdir 联机帮助页所述:

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

该模式也由当前 umask 修改,您可以使用 umask() 更改它。

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

现在,查看 umask() 联机帮助页,列出的评论之一证实了我的内心想法:

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

“最好在创建文件后使用 chmod() 更改文件权限。”

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.

设置您的 umask,以便为您的用户创建私有文件,然后使用 chmod 打开它们。

回答by Battle_707

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

在创建目录之前尝试调用此函数: clearstatcache(); 另外,如果您切换到另一个用户,也许您应该检查是否可以仅使用 mkdir 来完成。