umask

(PHP 3, PHP 4, PHP 5)

umask -- 改变当前的 umask

说明

int umask ( [int mask] )

umask() 将 PHP 的 umask 设定为 mask & 0777 并返回原来的 umask。当 PHP 被作为服务器模块使用时,在每个请求结束后 umask 会被恢复。

无参数调用 umask() 会返回当前的 umask。

注: 在多线程的服务器上尽量避免使用这个函数。创建文件后要改变其权限最好还是使用 chmod()。使用 umask() 会导致并发程序和服务器发生不可预知的情况,因为它们是使用相同的 umask 的。

例子 1. umask() 例子

<?php
$old
= umask(0);
chmod("/path/some_dir/some_file.txt", 0755);
umask($old);

// Checking
if ($old != umask()) {
    die(
'An error occured while changing back the umask');
}
?>


add a note add a note User Contributed Notes
ShaD@TW
17-May-2006 12:32
Notice that directory(s) and file(s) sometimes have different results.

<?php
umask
(0670);                    //- set umask
$handle = fopen('file', 'w');  //- 0006
mkdir("/path/dir");            //- 0107
?>

calculate the result:
<?php
$umask
= 0670;
umask($umask);
//- if you are creating a new directory, $permission = 0777;
//- if you are creating a new file, $permission = 0666.
printf( "result: %04o", $permission & ( 0777 - $umask) );
?>

BTW, as the manual said, the form of umask() is "int umask ( [int mask] )", so if you want to print/echo any umask, don't forget to convert it from DEC (because it returns a "int") to OCT.

<?php
$umask
= umask();          //- returns the current umask, which is a "int"
$umask = decoct($umask);  //- Now, $umask is a "string"
echo $umask;
?>

Don't forget that the argument(parameter) is a "int", too.

<?php
umask
(777);    //- WRONG! Even though you maybe use "umask 777" in some OS.
umask(0777);  //- OK
?>

If there was any mistake, please correct my statement.
19-Feb-2006 12:31
Using (cmask - umask) is a wrong way to calculate the new mask:

0022 - 0700 = 0656 WRONG
0700 & ~0022 = 0700 CORRECT

Correct php code:
<?php
$rmask
= ($cmask & ~$umask);
?>
andi<at>splitbrain.org
04-Mar-2005 06:03
To play around with umasks and permissions use this little fragment:

<?
$umask
= 0012;
$perm  = 0777;
printf("umask: %04o perm: %04o result: %04o\n",
      
$umask,$perm,$perm & (0777 - $umask));
?>
trisk at earthling dot net
01-Feb-2005 01:25
I thought I would clarify the numbering scheme used here, as it confused me at first.

On the UNIX console, the command:

umask "blah"

In this instance, the umask command forces "blah" to be an octal number, regardless of how many digits you use and regardless of any leading zeroes.  In PHP, umask() does not default to octal as the console command does, it uses whatever numeric format you specify.

For example:

umask(213);

This uses the decimal integer 213 and not the octal number 213 as you would expect when using the console command.  In this case, it would set the umask to the octal number "325".

To enter the number as octal, just add one or more zeroes to the left of the number:

umask(0213);
umask(07);
umask(0044);

etc.
notepad at codewalkers dot com
19-Jun-2004 09:43
$old = umask(0);
chmod("/some/dir", 0755);
umask($old);
sam at totallydigital dot co dot nz
20-Sep-2002 12:04
The first comment perhaps didn't quite make clear what's on with your umask and the permissions.

The permission passed to a command is first bitwise ANDed with the _INVERSE_ of the current umask, then applied to the file.

For example, umask = 0011 and permission = 0775
The inverse of 0011 = 0766

0775 AND 0766
= 111.111.101 AND 111.110.110
= 111.110.100
= 0764
wptate at olemiss dot edu
01-Mar-2000 05:19
The -S option will print the symbolic values of umask, while changing the umask to the new setting. 
(i.e. umask -S 007 will print 'u=rwx,g=rwx,o=')