PHP chmod() Function

What is PHP chmod() Function?

If you want to change permission (mode) of a file or directory, use chmod() function. With this function, you can set who can access a file and what they can do with this file. “chmod” is short for “Change mode”.

Syntax:

chmod(filename, permission)

Parameters:

The Function has 2 required parameters-

filename (Required): It is the path to the file or directory whose permission you want to change. Check example 1.

permission (Required): It is the permission that you want to set to the file/directory. You can specify the permission in one of two ways – Symbolic notation and Octal notation. In the following “How Permission Works” section, you’ll learn how to set permission with Symbolic and Octal notation.

Return Values:

The function returns-

  • TRUE – if it can change the permission.
  • FALSE – if it can’t change the permission. It may happen for different reasons – user doesn’t have to alter the permission, file/folder doesn’t exist, server doesn’t permit it. Check example 2.

How Permission Works?

In Unix based operating system, the file control mechanism determines –

  • Class: who can access a file/folder and
  • Permission: what they can do with it.

Class: There are three user classes of a file/folder –

  • Owner: Owner of the file/folder,
  • Group: Those who share the same permission, and
  • Others: All the users except owner and group.

Permission: It determines what a user class can do with a file/folder. Unix based operating systems implement three levels of permission to each user class-

  • Read: User class with read permission can only read the file or view the content of the folder.
  • Write: User class with write permission can edit a file. In case a folder, the user class can add a file, delete a file and can also rename a file.
  • Execute: User class with execute permission can run a file. In case of folder, the user class can search a folder with file name. Note to list the file inside a folder, the user class need read permission.

Permission representation: You can represent Unix permission one of two ways –

  • Symbolic notation and
  • Octal notation.

Symbolic permission: In the symbolic permission method, permissions are represented by-

  • r: which specifies Read permission,
  • w: which specifies Write permission, and
  • e: which specifies Execute permission.

And, the user classes are represented by-

  • u: which specifies owner (user) of the file/folder,
  • g: which specifies group who share the same permission,
  • o: which specifies others (all the users except owner and group), and
  • a: which specifies all (u, g, and o)

To apply above permission to above user classes, we use the following operators-

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

Let’s take a look at some symbolic notation permission examples-

  • u+rwx,g+r,o+r : This sets read, write, and execute permission to the owner, only read permission to the group and only read permission to the others for the specified file or folder.
  • u+rwx,go+r : Same as the previous one.

Octal notation: Unix permission can be represented in octal notation. Basically, it has three digits like 775 or 777. In it-

  • First one is for owner,
  • Second is for group, and
  • Third one is for others.

In this octal numeral system (which uses 0 to 7 digits)-

  • 4 represents “Read” permission,
  • 2 represents “Write” permission,
  • 1 represents “Execute” permission, and
  • 0 represents “No” permission.

You can sum above numbers to assign permissions to a user class. For example, to provide only “Read” and “Execute” permissions to a user group, add 4+1 = 5. Thus assign –

  • 0 (0+0+0) for “No” permission.
  • 1 (0+0+1) for “Execute” permission.
  • 2 (0+2+0) for “Write” permission.
  • 3 (0+2+1) for “Write” & “Execute” permissions.
  • 4 (4+0+0) for “Read” permission.
  • 5 (4+0+1) for “Read” & “Execute” permissions
  • 6 (4+2+0) for “Read” & “Write” permissions
  • 7 (4+2+1) for “Read”, “Write”, and “Execute” permissions.

Let’s take a look at some octal notation permission examples-

  • 775: This sets read, write, and execute permission to the owner and the group and read and execute permission to the others for the specified file or folder.
  • 700: This sets read, write, and execute permission to the owner and no permission to group and others.

Examples:

Example 1:

<?php
chmod("docs/dl.pdf", 0775)
?>

Explanation:

The function set permission 775 for the file “dl.pdf”. Here, in the second parameter, first digit “0” indicates that this second parameter is a octal number. The second and third digits indicate read, write, and execute permission to the owner and the group respectively. And the third parameter “5” indicates read and execute permission to the others.

Example 2:

<?php
var_dump(chmod("docs/dl2.pdf", 0775));
?>

Output:

Warning: chmod(): No such file or directory in D:\xampp\htdocs\php\chmod.php on line 2
bool(false)

Explanation:

As there is no file named “dl2.pdf” in the specified path, the function returns the warning and no permission is set also.

Best Practices on chmod() Function:

  • The function may not change the permission to the file/folder if there is any issue. So, keep your code safe by confirming its success with conditional statement. Check the following example-
  • Allow exact permission to a file/folder which is required. Don’t provide excessive permission which may put you at danger.
  • Change permission only when required.

Caution:

  • Allowing careless access permission to the sensitive file/folder may put your application at risk. So, always pay attention to the permission.
  • String permission may not work properly like “0+r”.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP chmod() Function

chmod() is one of the built in PHP file system functions that ensures proper file permissions and security of the files and folders in your web applications.

Reference:

https://www.php.net/manual/en/function.chmod.php