PHP chmod() Function

Definition and Usage

The chmod() function changes the file mode.

Returns TRUE on success, 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
// The owner can read and write, others have no permissions
chmod("test.txt",0600);
// The owner can read and write, others can read
chmod("test.txt",0644);
// The owner has all permissions, others can read and execute
chmod("test.txt",0755);
// The owner has all permissions, the group the owner belongs to can read
chmod("test.txt",0740);
?>