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);
Call
What 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
Parameter
Description
filename
Required. The path to the file or folder.
permissions
Required. The new mode as an octal integer, for example 0644. A leading zero is needed.
Return Value
Return Value
Description
bool
true 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.