Current Location: Home> Latest Articles> How to Use PHP's chmod Function to Handle Octal Permission Values?

How to Use PHP's chmod Function to Handle Octal Permission Values?

gitbox 2025-08-25

In Linux and Unix-like systems, file and directory permissions are represented by three-digit octal numbers (such as 0755, 0644). These values define the read, write, and execute rights for the file owner, the user group, and other users. In PHP, the chmod() function allows us to change file or directory permissions programmatically. This article will explain how to correctly use chmod(), with a focus on handling octal permission values.

1. Basic Syntax

<span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-title function_ invoke__">chmod</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$filename</span></span><span> , </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$permissions</span></span><span> )
</span></span>
  • $filename: The path of the file or directory whose permissions you want to change.

  • $permissions: The octal permission value (e.g., 0755).

The function returns a boolean value: true on success and false on failure.

2. Understanding Octal Permission Values

Octal permission values are usually written as three or four digits, where the first three digits represent:

  • First digit: Owner permissions

  • Second digit: Group permissions

  • Third digit: Other users’ permissions

Each digit corresponds to:

  • 4 = Read (r)

  • 2 = Write (w)

  • 1 = Execute (x)

These values can be combined. For example:

  • 7 = 4 + 2 + 1 = Read, Write, Execute

  • 6 = 4 + 2 = Read, Write

  • 5 = 4 + 1 = Read, Execute

Examples:

  • 0755 means: the owner can read, write, and execute; group and others can read and execute.

  • 0644 means: the owner can read and write; group and others can only read.

Note: In PHP, permission values must start with 0 to be interpreted as octal. Otherwise, they will be treated as decimal, leading to incorrect results.

<span><span><span class="hljs-title function_ invoke__">chmod</span></span><span>(</span><span><span class="hljs-string">&#039;example.txt&#039;</span></span><span>, </span><span><span class="hljs-number">0755</span></span><span>); </span><span><span class="hljs-comment">// Correct, 0755 is octal</span></span><span>
</span><span><span class="hljs-title function_ invoke__">chmod</span></span><span>(</span><span><span class="hljs-string">&#039;example.txt&#039;</span></span><span>, </span><span><span class="hljs-number">755</span></span><span>);  </span><span><span class="hljs-comment">// Incorrect, 755 is treated as decimal</span></span><span>
</span></span>

3. Permission Setting Examples

Here are some common permission settings:

<span><span><span class="hljs-comment">// Set to readable, writable, and executable by everyone</span></span><span>
</span><span><span class="hljs-title function_ invoke__">chmod</span></span><span>(</span><span><span class="hljs-string">&#039;script.sh&#039;</span></span><span>, </span><span><span class="hljs-number">0777</span></span><span>);
<p></span>// Set to owner read/write, others read-only<br>
chmod('config.ini', 0644);</p>
<p>// Set to owner read/write/execute, group and others read/execute<br>
chmod('public/index.php', 0755);<br>
</span>

4. Checking Permissions with fileperms()

You can also use the fileperms() function to get the current permissions of a file and convert them to octal format using decoct():

<span><span><span class="hljs-variable">$perms</span></span><span> = </span><span><span class="hljs-title function_ invoke__">fileperms</span></span><span>(</span><span><span class="hljs-string">&#039;example.txt&#039;</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">substr</span></span><span>(</span><span><span class="hljs-title function_ invoke__">decoct</span></span><span>(</span><span><span class="hljs-variable">$perms</span></span><span>), -</span><span><span class="hljs-number">4</span></span><span>); </span><span><span class="hljs-comment">// Outputs the last 4 octal digits</span></span><span>
</span></span>

This is very useful for debugging and verifying whether permission changes are applied correctly.

5. Important Notes

  1. The PHP script must have permission to modify the target file’s permissions. Otherwise, chmod() will fail.

  2. On some systems, directory permissions also affect file access.

  3. Avoid using 0777 permissions unless for temporary files or debugging. In production environments, files should not be writable by all users.

Conclusion

Mastering the use of PHP’s chmod() function, especially handling octal permission values, is essential for managing server file permissions and building secure web applications. As long as you remember to use octal format and assign permissions properly, you can avoid most file access and security issues.