PHP File Permissions (original) (raw)

Skip to content

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:

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(f(f(filename) ? 'The file ' . filename.filename . 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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo separator="true">,</mo><mi>i</mi><mi>n</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">filename , int </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span></span></span></span>permissions ) : boolCode language: PHP (php)

The chmod() function has two parameters:

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 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:

<?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 #

Did you find this tutorial useful?