Open In App

PHP chmod( ) Function

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax: 

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 need 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 parameter, for example, chmod (file.txt, “0744”), then PHP will do an implicit conversion to integer data type.

Examples:  

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:  

PHP




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


Output: 

true

Program 2

PHP




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


Output: 

true

Program 3

PHP




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


Output: 

true

Reference: 
http://php.net/manual/en/function.chmod.php
 



Last Updated : 26 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads