Skip to content

Instantly share code, notes, and snippets.

@muratpurc
Created September 22, 2011 10:12
Show Gist options
  • Save muratpurc/1234469 to your computer and use it in GitHub Desktop.
Save muratpurc/1234469 to your computer and use it in GitHub Desktop.
PHP: Change mode of folders/files recursively
/**
* Changes mode of folders/files recursively.
*
* @param string $dir The directory to change mode recursively
* @param int $dirMode Folder permissions to set
* @param int $fileMode File permissions to set
* @return void
*/
function mp_recursiveChmod($dir, $dirMode=0775, $fileMode=0775)
{
// first of all, change the root
$file = new SplFileInfo($dir);
if ($file->isDir()) {
chmod($file->getPathname(), $dirMode);
} else {
chmod($file->getPathname(), $fileMode);
}
// get the rest and change everything recursively
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST) as $file) {
if ($file->isDir()) {
chmod($file->getPathname(), $dirMode);
} else {
chmod($file->getPathname(), $fileMode);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment