Created
September 22, 2011 10:12
-
-
Save muratpurc/1234469 to your computer and use it in GitHub Desktop.
PHP: Change mode of folders/files recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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