PHP Function Reference

PHP umask() Function



The PHP umask() function changes permissions for files. It sets PHP's umask to mask & 0777 and returns the old umask. When PHP is being used as a server module, the umask is restored when each request is finished.

Syntax

umask(mask)

Parameters

mask Optional. Specify the new umask. Default is 0777. The parameter consists of four numbers:
  • The first number is always zero (octal value)
  • The second number specifies permissions for the OWNER
  • The third number specifies permissions for the OWNER's USER GROUP
  • The fourth number specifies permissions for EVERYBODY ELSE
Possible values (to set multiple permissions, add up the following numbers):
  • 1 = execute permissions
  • 2 = write permissions
  • 4 = read permissions

Return Value

Returns current umask if the function is called without arguments, otherwise the old umask is returned.

Example: umask() example

Lets assume that we have a file called test.txt in the current directory. In the example below, the umask() function is used to change the permissions for this file.

<?php
$old = umask(0);

//changing access
chmod("test.txt", 0755);

umask($old);

if ($old != umask()) {
  die('An error occurred while changing back the umask');
} else {
  echo "umask is changed back successfully.";
}
?>

The output of the above code will be:

umask is changed back successfully.

❮ PHP Filesystem Reference