Php umask (0) what is the purpose

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

I have seen this several times and cannot understand from the documentation what it is exactly doing.

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

+46
php
Aug 24 '12 at 20:23
source share
2 answers

Setting umask to 0000 (or just 0 ) means that previously created files or directories created will not have any rights that were originally revoked. In other words, a umask zero will cause all files to be created as 0666 or writable in the world. Directories created in umask 0 will be 0777 .

Usually, when you see umask(0) , it should follow the chmod() call directly to explicitly set the permissions needed for the newly created file or directory for something other than being written to the world.

Use caution when setting umask to zero! This can be dangerous and basically useful only for creating files that later need to be written by the web server when the web server is running as another user, who is a β€œreal” user who will also need to be able to modify files created by the web server . Otherwise, the default system umask is likely to be 0022 , writable by the file owner, but not by others. In this case, if you are logged in under a normal user account, the file created by the web server under PHP will not be available to you.

Instead of creating files that are written to the world, it is usually better to manage directories that the web server writes more explicitly. If files created inside a directory must have certain group permissions, it may be advisable to set the sgid bit in the directory so that new files inside it inherit group ownership. Users who need access to the file must be members of the group that has access to it. This is much safer than creating read-only files written to the world.

 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 
+42
Aug 24 2018-12-12T00:
source share

umask is basically the default setting for creating files. In essence, it is safer to enable your chmod by default and then write the file than write it and then chmod (some say). At the end of the day, the time between writing a file and running chmod would be, in the best case, in milliseconds, so I am skeptical about this.

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

It basically sets the default write permissions for everything that you insert into the OS, such as Linux, and then returns the old one so that you can reset it back. This applies to the PHP process itself.

Comments on the document page: http://php.net/manual/en/function.umask.php will be explained with an example.

+3
Aug 24 2018-12-12T00:
source share



All Articles