PHP Filesystem chmod() Function



The PHP Filesystem chmod() function can change permissions of a specified file. As the name suggests change mode which means it tries to change the mode of a file specified. It returns true on success, otherwise false on failure.

Important Points

  • The mode is not automatically assumed to an octal value, so strings (such as "g+w") can not work properly. To ensure the expected operation, we need to prefix mode with zero (0).
  • The parameter "mode" consists of three octal number components: access restrictions for the owner, user group in which the owner is in, and everybody else in this order.
  • Number 1 means that we grant execute permissions, number 2 means that we make the file writeable, and number 4 means that we make the file readable. We can add these numbers to specify the needed rights.

Syntax

Below is the syntax of the PHP Filesystem chmod() function −

bool chmod ( string filename, int permissions )

Parameters

Here are the required and optional parameters of the chmod() function −

Sr.No Parameter & Description
1

filename(Required)

The path to the file.

2

permissions(Required)

It is the permission that you want to change. Octal notation and symbolic notation are the two ways permission can be given.

Return Value

It returns TRUE value on success, or FALSE on failure.

Permission Representation

There are two ways to give permission −

  • Octal Notation
  • Symbolic Permission

Octal Notation

Octal notation is a way to express Unix permission. It basically consists of three digits, like 777 or 775. Within it - The owner is the first number, the group is the second, and the others have the third.

Using 0 to 7 digits, this octal number system is as follows −

  • 0 indicates "No" permission,
  • 1 indicates "Execute" permission,
  • 2 indicates "Write" permission,
  • 4 indicates "Read" permission,

To provide a user class permissions, add up the numbers above. For example, add 4+1 = 5 to grant a user group only "Read" and "Execute" permissions. Let us see some examples below −

  • 0 (0+0+0) for the permission "No".
  • 1 (0+0+1) for permission to "Execute".
  • 2 (0+2+0) for permission to "Write."
  • 3 (0+2+1) for the permissions to "Write" and "Execute".
  • 4 (4+0+0) for permission to "Read".
  • 5 (4+0+1) for the permissions to "Read" and "Execute".
  • 6 (4+2+0) for the permissions to "Read" and "Write".
  • 7 (4+2+1) for the permissions "Read," "Write," and "Execute".

Some of the octal notation permissions are −

  • 775: This gives the owner and the group read, write, and execute permissions, and it gives the others read and execute permissions for the given file or folder.
  • 700: This gives the owner read, write, and execute permission while denying others and groups access.

Symbolic Permission

Permissions are represented in the symbolic permission method as follows −

  • r: Read permission,
  • w: Write permission,
  • e: Execute permission.

The user classes are further represented by −

  • u: owner (user) of File or Folder.
  • g: group with identical permissions.
  • o: All other users (apart from the owner and group).
  • a: it includes all (u, g, and o).

We use below operations to provide the above permission to the user classes −

  • + : add access,
  • - : remove access,
  • = : set exact access.

PHP Version

The chmod() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.

Example

In the below code we have used PHP Filesystem chmod() function to set a permission 0600 which provides read and write for the owner, nothing to everybody else.

<?php
   // File path
   $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile1.txt';

   // Owner can read and write, others have no permissions
   $permissions = 0600; 

   if (chmod($file, $permissions)) {
      echo "Permissions has changed successfully for myfile1.txt.";
   } else {
      echo "Failed to change permissions for myfile1.txt.";
   }
?>

Output

This will produce the following result −

  • If permission is granted.

    Permissions has changed successfully for myfile1.txt.
    
  • If permission is denied.

    Failed to change permissions for myfile1.txt.
    

Example

In the given example we will use chmod() function to set a permission 0644 which provide read and write for owner and read-only for others.

<?php
   // File path
   $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile2.txt';

   // read and write for owner and read-only for others
   $permissions = 0644; 

   if (chmod($file, $permissions)) {
      echo "Permissions has changed successfully for myfile2.txt.";
   } else {
      echo "Failed to change permissions for myfile2.txt.";
   }
?>

Output

This will generate the below output −

  • If permission is granted.

    Permissions has changed successfully for myfile2.txt.
    
  • If permission is denied.

    Failed to change permissions for myfile2.txt.
    

Example

In the below example we have used chmod() function to set a permission 0755 which provide everything for the owner and read and execute for others. Because there are some conditions where the owner need to provide the read and execute ownership to others.

<?php
   // File path
   $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile3.txt';

   // everything for the owner and read and execute for others
   $permissions = 0755; 

   if (chmod($file, $permissions)) {
      echo "Permissions has changed successfully for myfile3.txt.";
   } else {
      echo "Failed to change permissions for myfile3.txt.";
   }
?>

Output

This will produce the following outcome −

  • If permission is granted.

    Permissions has changed successfully for myfile3.txt.
    
  • If permission is denied.

    Failed to change permissions for myfile3.txt.
    

Example

In the below example we have used chmod() function to set a permission 0740 which provide everything for owner, read for owner's group, and no permissions for others.

<?php
   // File path
   $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile4.txt';

   // everything for owner, read for owner's group, and no permissions for others
   $permissions = 0740; 

   if (chmod($file, $permissions)) {
      echo "Permissions has changed successfully for myfile4.txt.";
   } else {
      echo "Failed to change permissions for myfile4.txt.";
   }
?>

Output

The result of this PHP code is −

  • If permission is granted.

    Permissions has changed successfully for myfile4.txt.
    
  • If permission is denied.

    Failed to change permissions for myfile4.txt.
    

Note

If there is a problem, the function may or may not be able to modify the file or folder's permissions. So, use a conditional statement to make sure the success of your code and to keep it safe.

Summary

The built-in PHP Filesystem function chmod() checks that the directories and files in your web applications are safe and have the right permissions.

php_function_reference.htm
Advertisements