• PHP Video Tutorials

PHP - Function chmod()

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


bool chmod ( string filename, int mode )

Attempts to change the mode of a file specified by filename to that given in mode.

The mode is not automatically assumed to an octal value, so strings (such as "g+w") can not work properly. To ensure the expected operation, we need to prefix mode with zero (0).

The parameter "mode" consists of three octal number components: access restrictions for the owner, user group in which the owner is in, and everybody else in this order. Number 1 means that we grant execute permissions, number 2 means that we make the file writeable, and number 4 means that we make the file readable. We can add these numbers to specify the needed rights.


   // Read and write for owner, nothing for everybody else
   chmod("/PhpProject/sample.txt", 0600);

   // Read and write for owner, read for everybody else
   chmod("/PhpProject/sample.txt", 0644);

   // Everything for owner, read and execute for everybody else
   chmod("/PhpProject/sample.txt", 0755);

   // Everything for owner, read for owner's group
   chmod("/PhpProject/sample.txt", 0740);