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 a user can do with a file, 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 the read and write permissions.

PHP provides some useful functions for checking and changing the file permissions.

Checking file permissions

PHP has three handy functions that check file permissions:

  • is_readable() function returns true if the file exists and is readable; otherwise, it returns false.
  • is_writable() function returns true if the file exists and is writable; otherwise, it returns false.
  • is_executable() function returns true if the file exists and executable; otherwise, it returns false.

Let’s take a look at the following example:

<?php $filename = 'readme.txt'; $functions = [ 'is_readable', 'is_writable', 'is_executable' ]; foreach ($functions as $f) { echo $f($filename) ? 'The file ' . $filename . $f : ''; }
Code language: HTML, XML (xml)

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

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

Changing file permissions

To change the file permission or mode, you use the chmod() function:

chmod ( string $filename , int $permissions ) : bool
Code language: PHP (php)

The chmod() function has two parameters:

  • $filename is the file that you want to change the permissions.
  • $permissions parameter consists of three octal number components that specify access restrictions for the owner, the user group in which the owner is in, and everyone else in this sequence.

The chmod() function returns true on success or false on failure.

The permissions argument is represented by an octal number that contains three digits:

  • The first digit specifies what the owner of the file can read, write, or execute the file.
  • The second digit specifies what the user group in which the owner is in can read, write, or execute the file.
  • The third digit specifies what everyone else can read, write, or execute the file.

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

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

The following example sets permission that the only owner can read and write a file, everyone else only can read the file:

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

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

Summary

  • Use the is_readable(), is_writable(), is_executable() to check if a file exists and readable, writable, and executable.
  • Use the chmod() function to set permissions for a file.
Did you find this tutorial useful?