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 returnstrue
if the file exists and is readable; otherwise, it returnsfalse
.is_writable()
function returnstrue
if the file exists and is writable; otherwise, it returnsfalse
.is_executable()
function returnstrue
if the file exists and executable; otherwise, it returnsfalse
.
Let’s take a look at the following example:
$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:
$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) :
Value | Permission |
---|---|
0 | cannot read, write or execute |
1 | can only execute |
2 | can only write |
3 | can write and execute |
4 | can only read |
5 | can read and execute |
6 | can read and write |
7 | can 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:
$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.