Change file permissions recursively using php

You often ran into a position where you need to deploy your script on hosting and you have only ftp access. Your web server configuration may need to have right permissions on files to execute them.

Normally on linux servers directory permissions are set to 0755 and file permissions are 644, but if security is configured with apache it may require 0755 permissions on the files as well.

To change file permissions using ftp is a hectic job, it takes too much time to change permissions.

There is a simple solution in PHP. Just create a permissions file write the following code in it and put it in root directory and run the file, it will change all the files/directory permissions recursively with ease.

Create a file “chmod.php” and then put it in root directory.

function chmod_r($path, $perm) {
    $dir = new DirectoryIterator($path);
    foreach ($dir as $item) {
        chmod($item->getPathname(), 0755);
        if ($item->isDir() && !$item->isDot()) {

$path = getcwd(); 
//$path = "rental";
chmod_r($path, 0755);

echo "Permissions changed.";

run that file in your browser like
It will change all files/directories permissions.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s