Working with File Permissions in PHP

File system permissions determine what different users can do with each file and directory in the file system. For example, whereas one user might have permission to read and write to a file, another user may only be allowed to read the file. A third user might not even be allowed to do that.

Permissions generally won’t affect you much when writing PHP scripts, because PHP usually does the right thing behind the scenes. For example, if you create a new file for writing, PHP automatically gives that file read and write permission for the user that’s running your PHP script (usually the Web server user). If you create a new directory, PHP gives the directory read, write, and execute permission for all users by default, meaning that anyone can create and delete files within that directory.

Here, you will learn the PHP chmod() function, which allows you to change the mode (permissions) of a file or directory.
Here ,you will see the three PHP functions that let you determine if a file or directory is readable, writable, or executable by the current user.

Changing Permissions

PHP chmod() function is used to change the mode, or permissions, of a file or directory. It functions much like the UNIX chmod command.
This applies mainly to UNIX – based Web servers such as Linux and Mac OS X. Windows servers do not have a concept of file and directory modes. Instead, you use Windows Explorer to set access permissions on files and folders by right – clicking the item, choosing Properties, then clicking the Security tab. You need to be an administrator to make these changes. If you are running the PHP scripts on a shared Windows server, and you need to set permissions on a certain file or folder, ask your hosting company for help. Often they will do it for you, or point you to a Web – based control panel where you can do it yourself.

To change a file’s permissions with chmod() , pass it the filename and the new mode to use.

Example

To set a file’s mode to 644, use:

chmod( “phpdata.txt”, 0644 );

The 0 (zero) before the 644 is important, because it tells PHP to interpret the digits as an octal number.
chmod() returns true if the permission change was successful, and false if it failed.

File modes are usually expressed as octal numbers containing three digits. The first digit determines what the file’s owner usually the user that created the file can do with the file. The second digit determines what users in the file’s group again, usually the group of the user that created the file can do with it. Finally, the last digit determines what everyone else can do with the file.

The value of each digit represents the access permission for that particular class of user, as follows:

Digit Value Permission

  • 0 Cannot read, write to, or execute the file
  • 1 Can only execute the file
  • 2 Can only write to the file

Digit Value Permission

  • 3 Can write to and execute the file
  • 4 Can only read the file
  • 5 Can read and execute the file
  • 6 Can read and write to the file
  • 7 Can read, write to, and execute the file

Some commonly used examples

// Owner can read and write the file; everyone else can just read it:
chmod( “phpdata.txt”, 0644 );
// Everyone can read and write the file:
chmod( “phpdata.txt”, 0666 );
// Everyone can read and execute the file, but only the owner can write to it:
chmod( “phpdata.txt”, 0755 );
// Only the owner can access the file, and they can only read and write to it:
chmod( “phpdata.txt”, 0600 );

Checking File Permissions

Before you do something to a file in the script, it can be useful to know what kinds of things the script can do with the file. PHP provides three handy functions to help out.

To check if you are allowed to read a file, use is_readable() , passing in the filename of the file to check.
Similarly, you can check that you are allowed to write to a file with is_writable() , and see if you can execute a file with is_executable() . Each function returns true if the operation is allowed, or false if it is disallowed.

Example

if ( is_readable( “phpdata.txt” )
{
echo “I can read phpdata.txt”;
}
if ( is_writable( “phpdata.txt” )
{   echo “I can write to phpdata.txt”;
} if ( is_executable( “phpdata.txt” )
{
echo “I can execute phpdata.txt”;
}

You can also use the fileperms() function to return an integer representing the permissions that are set on a file or directory.

Example

To print the octal value of the permissions on a file use:
chmod( “phpdata.txt”, 0644 ); echo substr( sprintf( “%o”, fileperms( “phpdata.txt”) ), -4 ); // Displays “0644”

(The call to substr() is used to return just the last four digits, because the other octal digits in the returned value are not relevant.)

Scroll to Top