PHP File Permissions

Summary: in this tutorial, you will learn how to deal with PHP file permissions, including checking and changing file permissions.

File permissions specify what you can do with a particular file in the system, e.g., reading, writing, or executing it. Notice that PHP automatically grants appropriate permissions behind the scenes. For example, if you create a new file for writing, PHP automatically grants read and write permissions.

In addition, PHP also provides some useful functions for checking and changing the file permissions.

PHP checking file permissions

PHP gives you three handy functions that allow you to check file permissions:

  • is_readable() returns true if you are allowed to read the file; otherwise, it returns false.
  • is_writable() returns true if you are allowed to write the file; otherwise, it returns false.
  • is_executable() returns true if you are allowed to execute the file; otherwise, it returns false.

Let’s take a look at the following example:

<?php $fn = './test.text'; $msg = is_readable($fn) ? $msg = 'File is readable' : $msg = 'File is not readable'; echo $msg . '<br/>'; $msg = is_writable($fn) ? $msg = 'File is writable' : $msg = 'File is not writable'; echo $msg . '<br/>'; $msg = is_executable($fn) ? $msg = 'File is executable' : $msg = 'File is not executable'; echo $msg . '<br/>';
Code language: HTML, XML (xml)

Besides those functions, PHP also provides the fileperms() function that returns an integer, which represents the permissions that are set on a particular file.

<?php $fn = './test.txt'; $fp = fileperms($fn); echo substr(sprintf('%o', $fp), -4); //0666
Code language: HTML, XML (xml)

PHP changing file permissions

To change the file permissions or mode, you use chmod() function. First, you need to pass the name of the file that you want to set permission. Second, you need to specify the desired permission. The chmod() function returns true if the permission was set successfully otherwise it returns false.

A file permission is represented by an octal number that contains three digits:

  • The first digit specifies what the owner of the file can do with file.
  • The second digit specifies what the owner group of the file can do with the file.
  • The third digit specifies what everyone can do with the file.

The following table illustrates the value of each digit that represents the access permission for particular users ( user, user group or everyone) :

ValuePermission
0cannot read, write or execute
1can only execute
2can only write
3can write and execute
4can only read
5can read and execute
6can read and write
7can read, write and execute

For example, to set a permission that only creator or owner can read and write a file, everyone else only can read the file, we use the following code:

<?php $fn = './test.txt'; chmod($fn, 0644);
Code language: HTML, XML (xml)

Notice that we put 0 before 644 to instruct PHP to treat it as an octal number.

In this tutorial, you have learned how to check file permissions using the is_readable(), is_writeable(), and is_executable() and also how to change the file permissions or modes using the chmod() function.

Did you find this tutorial useful?