php 为什么PHP不能创建777权限的目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3997641/
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
Why can't PHP create a directory with 777 permissions?
提问by Sjwdavies
I'm trying to create a directory on my server using PHP with the command:
我正在尝试使用 PHP 和以下命令在我的服务器上创建一个目录:
mkdir("test", 0777);
But it doesn't give full permissions, only these:
但它没有提供完全权限,只有这些:
rwxr-xr-x
回答by paxdiablo
The mode is modified by your current umask
, which is 022
in this case.
该模式由您当前的 修改umask
,022
在这种情况下。
The way the umask
works is a subtractive one. You take the initial permission given to mkdir
and subtract the umask
to get the actualpermission:
umask
作品的方式是一种减法。您将获得的初始许可mkdir
减去umask
以获得实际许可:
0777
- 0022
======
0755 = rwxr-xr-x.
If you don't want this to happen, you need to set your umask
temporarily to zero so it has no effect. You can do this with the following snippet:
如果您不希望发生这种情况,则需要将您的umask
临时设置为零,以免产生任何影响。您可以使用以下代码段执行此操作:
$oldmask = umask(0);
mkdir("test", 0777);
umask($oldmask);
The first line changes the umask
to zero while storing the previous one into $oldmask
. The second line makes the directory using the desired permissions and (now irrelevant) umask
. The third line restores the umask
to what it was originally.
第一行将 更改umask
为零,同时将前一行存储到 中$oldmask
。第二行使目录使用所需的权限和(现在不相关)umask
。第三行将 恢复umask
为原来的样子。
回答by Steve Weet
The creation of files and directories is affected by the setting of umask. You can create files with a particular set of permissions by manipulating umask as follows :-
文件和目录的创建受 umask 的设置影响。您可以通过操作 umask 来创建具有特定权限集的文件,如下所示:-
$old = umask(0);
mkdir("test", 0777);
umask($old);
回答by Stanislav Malomuzh
Avoid using this function in multithreaded webservers. It is better to change the file permissions with chmod() after creating the file.
避免在多线程 Web 服务器中使用此函数。创建文件后最好使用 chmod() 更改文件权限。
Example:
例子:
$dir = "test";
$permit = 0777;
mkdir($dir);
chmod($dir, $permit);
回答by Niket Pathak
For those who tried
对于那些尝试过的人
mkdir('path', 777);
mkdir('path', 777);
and it did not work.
它没有用。
It is because, apparently, the 0 preceding the file mode is very important which tells chmod to interpret the passed number as an Octal instead of a decimal.
这是因为,显然,文件模式前面的 0 非常重要,它告诉 chmod 将传递的数字解释为八进制而不是十进制。
Ps. This is not a solution to the question but only an add-on to the accepted anwser
附言。这不是问题的解决方案,而只是已接受的 anwser 的附加组件
回答by Mahesh Hegde
In my case, I have to use the following way for centos7, which solved the problem
就我而言,我必须对centos7使用以下方式,解决了问题
$oldmask = umask(000);//it will set the new umask and returns the old one
mkdir("test", 0777);
umask($oldmask);//reset the old umask
More details can be found at https://www.php.net/manual/en/function.umask.php