PHP Chmod Dir Function With Examples

What is PHP chmod?

PHP chmod() is a built-in function of the language that is useful for changing the mode of a specific file.

This function can change the mode of the specified file into a specific mode that the user wants.

Now, before we try to implement the function into an actual program, let us know the characteristics of the PHP chmod() dir function.

Syntax:

chmod( $file, int $mode): bool

Parameters

The PHP chmod() dir function takes two parameters which are the $file and the $mode.

File

The $file parameter is required by the chmod() function because it specifies the path or the filename of the file.

Mode

$mode parameter is also required since it specifies the new permissions which will be the basis for the file modification.

Additionally, this parameter consists of the following numbers:

  • The first digit is always zero. 
  • A second number determines the owner’s permission. 
  • This third number provides the owner’s user group rights. 
  • The fourth digit specifies authorization for everyone else.

Because mode is not automatically presumed to be an octal value, you must precede the mode with a zero to ensure the desired operation (0).

Another issue is that strings like “g+w” will not function properly.

The $mode parameter of PHP chmod() is made up of three octal number components that describe access limits for the owner, the user group that the owner belongs to, and everyone else, in that order.

To figure out one part, you can add up the permissions needed by the intended user base.

  • 1 denotes that you have granted executory rights.
  • 2 indicates that you have made the file writable.
  • 4 indicates that you have made the file viewable.

You can add these numbers together to determine the required privileges.

Moreover, you may also learn more about Unix modes by running “man 1 chmod” and “man 2 chmod.”

Return Values

The PHP chmod() dir function does the modification by attempting to change the permissions of the specified file.

This modification will return true if the modification was successful, and false if it was not.

Errors and Exception

PHP’s chmod() function does not operate on remote files. It will, however, work on files that are available via the server’s filesystem.

Furthermore, if quotes are used around the $mode argument, PHP will convert it to an integer data type implicitly.

When a program fails or encounters an error, the chmod() method displays an E_WARNING.

Example

In this section, we will have an example program where we can implement the concept of the PHP chmod() dir function.

Please note that each of the lines in the sample below has its purpose when used in an actual program.

Example Program:

<?php
chmod("sample.txt",0600);

chmod("sample.txt",0644);

chmod("sample.txt",0755);

chmod("sample.txt",0740);
?>

Program Explanation:

chmod 600 – This can read and write for the owner and nothing for everybody else.

chmod 644 – This can also read and write for the owner as well as read for everybody else.

chmod 755 – Can provide everything for the owner and only read and execute for everybody else.

chmod 740 – Does everything for the owner, and only reads for the owner’s group.

Summary

In summary, we have completed and delivered everything that a programmer needs to know the PHP chmod() dir function works.

This function will help them in dealing with their files such as modifying, reading, and writing them using the path that they provide.

However, if you have any concerns regarding this topic, you can always tap us through the comments below.

Leave a Comment