PHP chmod() Function

Definition and Usage

The chmod() function changes the file mode.

Returns TRUE if successful, otherwise returns FALSE.

Syntax

chmod(file,mode)
Parameters Description
file Required. Specifies the file to be checked.
mode

Optional. Specifies the new permissions.

The mode parameter consists of 4 numbers:

  • The first number is always 0
  • The second number specifies the owner's permissions
  • The second number specifies the permissions of the user group the owner belongs to
  • The fourth number specifies the permissions of other users

Possible Values (if you need to set multiple permissions, please sum the following numbers):

  • 1 - Execute Permission
  • 2 - Write Permission
  • 4 - Read Permission

Example

<?php
// Owner can read and write, others have no permissions
chmod("test.txt",0600);
// Owner can read and write, others can read
chmod("test.txt",0644);
// Owner has all permissions, others can read and execute
chmod("test.txt",0755);
// Owner has all permissions, the group the owner belongs to can read
chmod("test.txt",0740);
?>