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.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

true

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

true

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.