Skip to content
Related Articles

Related Articles

PHP | chmod( ) Function
  • Last Updated : 20 Apr, 2018

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 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.

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
  
// Read and write permission to owner
chmod("gfg.txt", 0600);
  
?>

Output:

true

Program 2:




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

Output:

true

Program 3:




<?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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :