Current Location: Home> Latest Articles> How to Use umask() and stat() in PHP to Monitor File Permission Changes: A Practical Guide

How to Use umask() and stat() in PHP to Monitor File Permission Changes: A Practical Guide

gitbox 2025-07-09

1. Introduction to the umask() Function

The umask() function is used to set or retrieve the current process’s file mode creation mask (umask). This mask determines the default permissions assigned to newly created files. By setting an appropriate umask, PHP scripts can ensure that new files have restricted permissions automatically.

Using umask()

<span><span><span class="hljs-comment">// Retrieve the current umask value</span></span><span>
</span><span><span class="hljs-variable">$current_umask</span></span><span> = </span><span><span class="hljs-title function_ invoke__">umask</span></span><span>();
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Current umask value: "</span></span><span> . </span><span><span class="hljs-title function_ invoke__">decoct</span></span><span>(</span><span><span class="hljs-variable">$current_umask</span></span><span>) . </span><span><span class="hljs-string">"\n"</span></span><span>;
<p></span>// Set a new umask value<br>
umask(0022);  // 0022 means new files will have 755 permissions<br>
</span>

In the example above, we first retrieve the current umask value, then set a new umask using the umask() function. The value 0022 means that newly created files will automatically be assigned 755 permissions (i.e., the owner has read/write/execute rights, while others have read and execute rights only).


2. Introduction to the stat() Function

The stat() function is used to retrieve information about a specified file, such as its size, last modification time, and permissions. It returns an array of detailed file attributes, with the mode field indicating the file’s permission bits.

Basic Usage of stat()

<span><span><span class="hljs-variable">$file_stat</span></span><span> = </span><span><span class="hljs-title function_ invoke__">stat</span></span><span>(</span><span><span class="hljs-string">'example.txt'</span></span><span>);
<p></span>// Display the file's permission mode<br>
echo "File permission mode: " . decoct($file_stat['mode']) . "\n";<br>
</span>

The mode value returned by stat() is an integer and needs to be interpreted using bitwise operations to extract the permission bits.


3. Using umask() and stat() Together to Monitor Permission Changes

To better manage file permissions, you can combine the use of umask() and stat() to track changes. For example, suppose you want to verify that a file created by your script has the correct permissions as expected.

Steps to Implement:

  1. Set the appropriate umask to control the default permissions of new files.

  2. Use the stat() function to retrieve the file’s permission information.

  3. Compare the permission value returned by stat() with your expected value to determine if there’s any deviation.

Example Code:

<span><span><span class="hljs-meta"><?php</span></span><span>
<p></span>// Set file permission mask<br>
umask(0022);</p>
<p>// Create a new file<br>
$file = 'example.txt';<br>
file_put_contents($file, "This is a test file.");</p>
<p>// Retrieve file stats<br>
$file_stat = stat($file);</p>
<p>// Extract permission bits<br>
$file_permissions = $file_stat['mode'] & 0777;  // Bitwise AND to isolate permission bits<br>
echo "Actual file permissions: " . decoct($file_permissions) . "\n";</p>
<p>// Check if permissions match expectations<br>
$expected_permissions = 0755;<br>
if ($file_permissions === $expected_permissions) {<br>
echo "File permissions match expectations!\n";<br>
} else {<br>
echo "File permissions do not match. Actual: " . decoct($file_permissions) . "\n";<br>
}</p>
<p>?><br>
</span>

Code Explanation:

  • First, umask(0022) is used to ensure that the file will have 0755 permissions.

  • A new file example.txt is created using file_put_contents().

  • We then retrieve the file’s stat() data, focusing on the mode value.

  • Finally, we apply a bitmask (& 0777) to extract permission bits and compare them with the expected value 0755.

Expected Output:

<span><span><span class="hljs-section">Actual file permissions: 755</span></span><span>
File permissions match expectations!
</span></span>

This method provides an effective way to verify that files are created with the correct permissions, enhancing the security of your PHP applications.