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 permissions of the owner
  • The second number specifies the permissions of the user group of the owner
  • The fourth number specifies the permissions of other users

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

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

Example

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