set permissions for all files and folders recursively set permissions for all files and folders recursively php php

set permissions for all files and folders recursively


Why don't use find tool for this?

exec ("find /path/to/folder -type d -exec chmod 0750 {} +");exec ("find /path/to/folder -type f -exec chmod 0644 {} +");


My solution will change all files and folder recursively to 0777. I use DirecotryIterator, it's much cleaner instead of opendir and while loop.

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


This is tested and works like a charm:

<?  header('Content-Type: text/plain');  /**  * Changes permissions on files and directories within $dir and dives recursively  * into found subdirectories.  */  function chmod_r($dir, $dirPermissions, $filePermissions) {      $dp = opendir($dir);       while($file = readdir($dp)) {         if (($file == ".") || ($file == ".."))            continue;        $fullPath = $dir."/".$file;         if(is_dir($fullPath)) {            echo('DIR:' . $fullPath . "\n");            chmod($fullPath, $dirPermissions);            chmod_r($fullPath, $dirPermissions, $filePermissions);         } else {            echo('FILE:' . $fullPath . "\n");            chmod($fullPath, $filePermissions);         }       }     closedir($dp);  }  chmod_r(dirname(__FILE__), 0755, 0755);?>