PHP umask() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, umask() reads or changes the default permission mask. The mask decides which permissions are removed from files and folders that your script creates.

A new file normally starts with permissions 0666 and a folder with 0777. PHP then removes the bits in the mask. With a mask of 0022, a new file ends up as 0644 and a folder as 0755.

Syntax

Call umask() with the values you need. Start with this line.

PHP
$old = umask(0022);
CallWhat it does
umask()Returns the current mask without changing it.
umask(0022)Sets the mask to 0022 and returns the old mask.
umask(0077)Makes new files private, so only the owner can read and write them.

The mask applies to the current process only. A mask is written in octal, so always start it with 0, for example 0022. On Windows the function exists but has no real effect on permissions.

Library declarations
PHP
umask(?int $mask = null): int

Parameters

ParameterDescription
maskOptional. The new mask in octal. If it is left out or null, the current mask is returned and nothing is changed.

Return Value

Return ValueDescription
intReturns the old mask when a new one is set. Without an argument, it returns the current mask.

PHP Examples

Example 1 — Set and Read the Mask

Set the mask to 0022 and read it back in octal.

PHP
<?php
$old = umask(0022);

printf("Current mask: %04o" . PHP_EOL, umask());

umask($old);

How It Works

  1. umask(0022) sets the new mask and returns the old one, which we save in $old.
  2. umask() without an argument returns the current mask.
  3. printf() with %04o prints the number in octal with four digits.
  4. The last line puts the original mask back.

Example 2 — Reading the Mask Does Not Change It

Calling umask() with no argument only reads the value. It is safe to call many times.

PHP
<?php
$old = umask(0027);

$first  = umask();
$second = umask();

echo $first === $second ? 'Mask did not change' : 'Mask changed', PHP_EOL;
printf("Mask: %04o" . PHP_EOL, $second);

umask($old);

How It Works

  1. The mask is set to 0027 first.
  2. Two calls to umask() without an argument read the same value.
  3. Both numbers are equal, so the first message is printed.
  4. The octal print shows 0027, and the original mask is restored.

Example 3 — Create a Private File

Use a strict mask so a new file is readable only by its owner.

PHP
<?php
$file = sys_get_temp_dir() . '/umask_demo.txt';

$old = umask(0077);
touch($file);
umask($old);

clearstatcache();
echo 'Permissions: ', substr(sprintf('%o', fileperms($file)), -4), PHP_EOL;

unlink($file);

How It Works

  1. umask(0077) removes all permissions for the group and others.
  2. touch() creates a new file, which starts at 0666 and loses the masked bits, leaving 0600.
  3. The old mask is restored right away so other code is not affected.
  4. fileperms() and sprintf('%o') show the permissions as 0600. On Windows this example will not show the same result.

Frequently Asked Questions

It reads or sets the permission mask for new files and folders created by the current process.
A file starts at 0666, and 0022 removes write permission for group and others, so the file becomes 0644. A folder starts at 0777 and becomes 0755.
No. chmod() sets exact permissions and ignores the mask. The mask only affects files and folders created afterwards.
Yes. The mask applies to the whole process, so save the value returned by umask() and set it back when your code is done.
The function exists but it has no practical effect on Windows, because Windows does not use Unix style permissions.

Next: unlink()

unlink() deletes a file from the file system.

unlink() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful