PHP | chmod() Function

PHP chmod() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP chmod() Function.

The built-in PHP chmod() function changes permissions of a specified file.

Syntax

Following below is the syntax to use this function -

bool chmod ( string filename, int mode )


This function tries to change the mode of the file specified by filename to that given in mode.

The mode is not automatically assumed to an octal value, so strings ( such as "g+w" ) can't work properly. To ensure the expected operation, then we need to prefix mode with zero.

The parameter mode consists of three octal number components: access restriction for the owner, user group in which the owner is in, and everybody else in this order. Number 1 means we grant an execute permissions, number 2 means that we have made the file writeable, and number 4 means we made the file to be readable. We can add all these numbers to specify the needed rights.

Return Value

This function returns TRUE on success and FALSE on failure.

Example

Try out the below example -

<?php
   // Read and write for owner, nothing for everybody else
   chmod("/PhpProject/sample.txt", 0600);

   // Read and write for owner, read for everybody else
   chmod("/PhpProject/sample.txt", 0644);

   // Everything for owner, read and execute for everybody else
   chmod("/PhpProject/sample.txt", 0755);

   // Everything for owner, read for owner's group
   chmod("/PhpProject/sample.txt", 0740);
?>


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP chown() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.