PHP Function Reference

PHP chmod() Function



The PHP chmod() function attempts to change permissions of the specified file and returns true on success or false on failure.

Note: The current user is the user under which PHP runs. The file permissions can be changed only by user who owns the file on most the systems.

Syntax

chmod(filename, permissions)

Parameters

filename Required. Specify the file to set permissions on.
permissions Required. Specify the new permissions, given as an octal value (starting with 0). The parameter consists of four numbers:
  • The first number is always zero (octal value)
  • The second number specifies permissions for the OWNER
  • The third number specifies permissions for the OWNER's USER GROUP
  • The fourth number specifies permissions for EVERYBODY ELSE
Possible values (to set multiple permissions, add up the following numbers):
  • 1 = execute permissions
  • 2 = write permissions
  • 4 = read permissions

Return Value

Returns true on success or false on failure.

Example: chmod() example

Lets assume that we have a file called test.txt in the current working directory. The example below describes how to use chmod() function to change permissions for this file.

<?php
$file = "test.txt";
  
//read and write for owner only
if(chmod("test.txt",0600))
  echo "Permissions is successfully set to 0600 \n";

//read and write for owner, read for everybody else
if(chmod("test.txt",0644))
  echo "Permissions is successfully set to 0644 \n";

//everything for owner, read and execute for everybody else
if(chmod("test.txt",0755))
  echo "Permissions is successfully set to 0755 \n";

//everything for owner, read for owner's group
if(chmod("test.txt",0740))
  echo "Permissions is successfully set to 0740 \n";
?>

The output of the above code will be:

Permissions is successfully set to 0600 
Permissions is successfully set to 0644 
Permissions is successfully set to 0755 
Permissions is successfully set to 0740 

❮ PHP Filesystem Reference