English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP chmod() Function Usage and Example

PHP Filesystem Reference Manual

The chmod() function can change the permissions of the specified file. It returns true on success, otherwise false.

Syntax

bool chmod ( string filename, int mode )

Try to change the mode of the file specified by filename to the mode given in mode.

Note that mode is not automatically treated as an octal number and cannot be used as a string (e.g., "g+w"). To ensure correct operation, you need to add 0 before mode

"mode" parameter contains three octal numbers in order, specifying the owner, the group the owner belongs to, and the access restrictions for everyone. Each part can be calculated to determine the required permissions by adding the required permissions. Number 1 Indicates making the file executable, number 2 Indicates making the file writable, number 4 Indicates making the file readable. Add these numbers to determine the required permissions.

Online Example

<?php
   //Owner can read and write, others cannot read or write
   chmod("/PhpProject/sample.txt", 0600);
   //Owner can read and write, others can read and write
   chmod("/PhpProject/sample.txt", 0644);
   //Owner can read and execute with others
   chmod("/PhpProject/sample.txt", 0755);
   //Owner, owner group readable
   chmod("/PhpProject/sample.txt", 0740);
?>