PHP | chmod( ) Function

The chmod() function in PHP is an inbuilt function which is used to change the mode of a specified file to a specific mode given by the user.
The chmod() function changes the permissions of the specified file and returns true on success and false on failure.


bool chmod ( string $filename, int $mode )

Parameters Used:
The chmod() function in PHP accepts two parameters which are filename and mode.

  1. $filename: It specifies the file whose permissions needs to be changed.
  2. $mode: It is used to specify the new permissions.

    The $mode parameters consist of four numeric values where the first value is always zero, the second value specifies permissions for the owner, the third value specifies permissions for the owner’s user group and the fourth value specifies permissions for everybody else.
    There are three possible values and to set multiple permissions the following values can be added.

    • 1 = execute permissions
    • 2 = write permissions
    • 4 = read permissions

Return Value: It returns true on successful execution and false on failure.

Errors And Exception:

  1. The chmod() function in PHP doesn’t works for remote files. It only works on files which are accessible by the server’s filesystem.
  2. If quotes are used around the $mode paarmeter, for example chmod (file.txt, “0744”), then PHP will do an implicit conversion to integer data type.


Input : chmod("gfg.txt", 0600);
Output : true

Input : chmod("gfg.txt", 0644);
Output : true

Input : chmod("gfg.txt", 0755);
Output : true

Below programs illustrate the chmod() function in PHP:

Program 1:

// Read and write permission to owner
chmod("gfg.txt", 0600);



Program 2:

// Read and write permission to owner, 
// and read permission to everyone else
chmod("gfg.txt", 0644);



Program 3:

// All permissions to owner, read and
// execute permissions to everyone else
chmod("gfg.txt", 0755);




This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter