PHP chmod() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, chmod() changes the permissions (the read, write, and execute rights) of a file or folder. The permissions are given as an octal number such as 0644 or 0755.

The leading zero matters because it tells PHP the number is octal. Without it, PHP reads the value as a normal decimal number and you get the wrong permissions. The function has limited effect on Windows.

Syntax

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

PHP
$ok = chmod(__DIR__ . "/demo.txt", 0644);
CallWhat it does
chmod($file, 0644)Owner can read and write, everyone else can only read.
chmod($file, 0755)Owner has full access, others can read and run it.
chmod($file, 0600)Only the owner can read and write the file.

Always write the mode with a leading 0, like 0644. chmod() only changes the permissions of a file you own (or any file if you are the super user), and it clears the file status cache.

Library declarations
PHP
chmod(string $filename, int $permissions): bool

Parameters

ParameterDescription
filenameRequired. The path to the file or folder.
permissionsRequired. The new mode as an octal integer, for example 0644. A leading zero is needed.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Make a File Read Only for Others

Create a file, set the mode to 0644, and read the mode back. The output below is for Linux or macOS.

PHP
<?php
$file = __DIR__ . "/demo.txt";
file_put_contents($file, "Hello");

chmod($file, 0644);

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

unlink($file);

How It Works

  1. file_put_contents() creates the demo file.
  2. chmod() sets the permissions to 0644.
  3. fileperms() reads the mode, and sprintf("%o") shows it in octal.
  4. substr() keeps the last four digits, which are the permission part.

Example 2 — Forgetting the Leading Zero

Writing 644 instead of 0644 is a common mistake. PHP treats 644 as a decimal number, which gives strange permissions.

PHP
<?php
$file = __DIR__ . "/demo.txt";
file_put_contents($file, "Hello");

chmod($file, 644);
clearstatcache();
echo "Wrong: ", substr(sprintf("%o", fileperms($file)), -4), PHP_EOL;

chmod($file, 0644);
clearstatcache();
echo "Right: ", substr(sprintf("%o", fileperms($file)), -4), PHP_EOL;

unlink($file);

How It Works

  1. chmod($file, 644) uses decimal 644, which is 1204 in octal.
  2. The first line shows the wrong mode that this causes.
  3. chmod($file, 0644) uses a real octal value.
  4. The second line shows the correct 0644 permissions.

Example 3 — Make a Script Executable

Set 0755 so the owner can run the file and others can read and run it.

PHP
<?php
$file = __DIR__ . "/run.sh";
file_put_contents($file, "#!/bin/sh" . PHP_EOL . "echo hi" . PHP_EOL);

echo "Before: ", is_executable($file) ? "yes" : "no", PHP_EOL;

chmod($file, 0755);
clearstatcache();

echo "After: ", is_executable($file) ? "yes" : "no", PHP_EOL;

unlink($file);

How It Works

  1. We create a small shell script file without execute rights.
  2. is_executable() reports no before the change.
  3. chmod($file, 0755) gives the owner execute rights.
  4. clearstatcache() makes sure PHP checks the real file again before is_executable().

Frequently Asked Questions

It changes the permissions of a file or folder, such as who can read, write, or run it. The mode is given as an octal number.
The leading zero tells PHP the number is octal. Without it, 644 is read as decimal and the result is a different, wrong mode.
Use 0644 for normal files and 0755 for folders. Use 0600 for private files such as config files with passwords.
Use fileperms($file) with sprintf("%o", ...) and substr() to show the last four octal digits, for example 0644.
It only has a small effect there. It can toggle the read-only flag, but the Unix style modes like 0755 are not fully supported.

Next: chown()

chown() changes the owner of a file or folder. You can give the user as a name such as www.

chown() 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