Using PHP chmod() to Set File Permissions
This article explains how to use PHP's chmod() function to change file permissions, describes permission masks, provides the syntax and a practical example that sets a file's mode to 644, and includes the full PHP code snippet.
In PHP, you can use the chmod() function to set a file's permissions. The chmod() function accepts two parameters: the first is the file path, and the second is the permission mask expressed in octal.
The permission mask is a three‑digit number that represents the owner, group, and others permissions. Each digit corresponds to read (4), write (2), and execute (1). For example, the mask 755 gives the owner read, write, and execute permissions, the group read and execute, and others execute only.
To use the chmod() function to set file permissions, the syntax is:
chmod($filename, $permissions);Where:
$filename is the path of the file whose permissions you want to set.
$permissions is the permission mask expressed in octal.
Practical Example:
The following example shows how to use chmod() to set the permissions of the file myfile.txt to 644 (owner read/write, group and others read only):
<?php
// Path of the file to set permissions
$filename = 'myfile.txt';
// Permission mask in octal form
$permissions = 0644;
// Use chmod() to set the file permissions
chmod($filename, $permissions);
?>After executing this script, the file myfile.txt will have its permissions set to 644.
PHP8 video tutorial
Scan the QR code to receive free learning materials
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.