Skip to content

Instantly share code, notes, and snippets.

@pnomolos
Created December 15, 2013 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pnomolos/7979177 to your computer and use it in GitHub Desktop.
Save pnomolos/7979177 to your computer and use it in GitHub Desktop.
Recursive chmod for PHP
<?php
function chmod_R($path, $filemode = null, $dirmode = null, $skip_hidden = true) {
foreach (glob($path, GLOB_MARK) as $path) {
if ($skip_hidden && strpos($path, '.') === 0) {
continue;
}
if (is_dir($path)) {
if ($dirmode && !chmod($path, $dirmode)) {
$dirmode_str=decoct($dirmode);
print "Failed applying filemode '$dirmode_str' on directory '$path'\n";
print " `-> the directory '$path' will be skipped from recursive chmod\n";
return;
}
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') { // skip self and parent pointing directories
$fullpath = $path.'/'.$file;
chmod_R($fullpath, $filemode,$dirmode);
}
}
closedir($dh);
} else {
if (is_link($path)) {
print "link '$path' is skipped\n";
return;
}
if ($filemode && !chmod($path, $filemode)) {
$filemode_str=decoct($filemode);
print "Failed applying filemode '$filemode_str' on file '$path'\n";
return;
}
}
}
}
@pnomolos
Copy link
Author

This was assembled from various snippets I've found in my internet travels and tweaked into its final for by me. Hope it helps someone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment