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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:41:03  来源:igfitidea点击:

Why can't PHP create a directory with 777 permissions?

phpmkdir

提问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 022in this case.

该模式由您当前的 修改umask022在这种情况下。

The way the umaskworks is a subtractive one. You take the initial permission given to mkdirand subtract the umaskto 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 umasktemporarily 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 umaskto 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 umaskto what it was originally.

第一行将 更改umask为零,同时将前一行存储到 中$oldmask。第二行使目录使用所需的权限和(现在不相关)umask。第三行将 恢复umask为原来的样子。

See the PHP doco for umaskand mkdirfor more details.

有关更多详细信息,请参阅umaskmkdir的 PHP doco 。

回答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 将传递的数字解释为八进制而不是十进制。

Reference

参考

Ps. This is not a solution to the question but only an add-on to the accepted anwser

附言。这不是问题的解决方案,而只是已接受的 anwser 的附加组件

回答by The Archetypal Paul

Probably, your umaskis set to exclude those

可能,您的umask设置为排除那些

回答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

更多细节可以在https://www.php.net/manual/en/function.umask.php找到