php umask(0) 的目的是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12116121/
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 02:51:38  来源:igfitidea点击:

php umask(0) what is the purpose

php

提问by Marty Wallace

What is the purpose of using umask(0);in php?

umask(0);在php中使用的目的是什么?

I have seen this a few times, and cant figure out from the documentation what it precisely does.

我已经看过几次了,无法从文档中弄清楚它究竟做了什么。

Can someone explain this and when it would be useful to use?

有人可以解释这一点以及何时使用它有用吗?

采纳答案by Michael Berkowski

Setting the umaskto 0000(or just 0) means that newly created files or directories created will have no privileges initially revoked. In other words, a umaskof zero will cause all files to be created as 0666or world-writable. Directories created while umaskis 0will be 0777.

设置umask0000(或只是0)意味着新创建的文件或创建的目录最初不会被撤销。换句话说,aumask为零将导致所有文件都被创建为0666或全局可写。在umaskis0时创建的目录将是0777

Usually when you see umask(0)it should be followed directly by a call to chmod()to explicitly set the permissions needed on the newly created file or directory to something other than world-writable.

通常,当您看到umask(0)它时,应直接调用 以chmod()将新创建的文件或目录所需的权限显式设置为非全局可写的内容。

Use caution when setting the umask to zero!This can be dangerous and is mostly only useful for creating files which must be later written to by the web server, when the web server runs as a different user that a "real" user who will also need to be able to modify the files created by the web server. Otherwise, the system's default umask is likely to be something like 0022, writable by the file owner but not others. In that case, if you logged into the machine under a normal user account, the file created by the web server under PHP would notbe writable by you.

将 umask 设置为零时要小心!这可能很危险,并且主要仅用于创建稍后必须由 Web 服务器写入的文件,当 Web 服务器作为不同用户运行时,“真实”用户也需要能够修改创建的文件通过网络服务器。否则,系统的默认 umask 可能类似于0022,文件所有者可写但其他人不能写。在这种情况下,如果您以普通用户帐户登录机器,那么您将无法写入由PHP 下的 Web 服务器创建的文件。

Rather than creating world-writable files, it is generally a better idea to manage the directories the web server is writing to more explicitly. If the files created inside a directory should have certain group permissions, it may be advisable to set the sgid bit on the directory so new files inside it inherit group ownership. Users needing access to the file should be members of the group having access to it. This is much more secure than creating world-readable, world-writable files.

与创建全局可写文件相比,更明确地管理 Web 服务器正在写入的目录通常是一个更好的主意。如果在目录中创建的文件应具有特定的组权限,则建议在目录上设置 sgid 位,以便其中的新文件继承组所有权。需要访问该文件的用户应该是有权访问该文件的组的成员。这比创建世界可读、世界可写的文件要安全得多。

php > umask(0);
// Should get created as 666
php > touch('file1.txt');

// "2" perms revoked from group, others, gets created as 644
php > umask(022);
php > touch('file2.txt');

// All revoked (2,4) from group, others, gets created as 600
php > umask(066);
php > touch('file3.txt');

-rw-rw-rw-   1 me  group     0 Aug 24 15:34 file1.txt
-rw-r--r--   1 me  group     0 Aug 24 15:35 file2.txt
-rw-------   1 me  group     0 Aug 24 15:37 file3.txt

回答by Sammaye

The umask is basically a default setting for creating files. In essence it is safer to turn your default chmod on and then write the file than write it and then chmod it (some say). At the end of the day the time between finishing writing the file and running chmod would be miliseconds at best so I am sceptical about this.

umask 基本上是创建文件的默认设置。从本质上讲,打开默认的 chmod 然后写入文件比写入然后 chmod 更安全(有人说)。在一天结束时,完成写入文件和运行 chmod 之间的时间最多只有几毫秒,所以我对此持怀疑态度。

umask() sets PHP's umask to mask & 0777 and returns the old umask

It basically sets default write permissions to whatever you put in on OS's such as Linux and then returns the old one so you can reset it back. This applies to the PHP process itself.

它基本上为您在 Linux 等操作系统上放置的任何内容设置默认写入权限,然后返回旧的权限,以便您可以将其重置。这适用于 PHP 进程本身。

The comments on the doc page: http://php.net/manual/en/function.umask.phpwill clarify by example.

文档页面上的评论:http: //php.net/manual/en/function.umask.php将通过示例进行澄清。