Created
December 15, 2013 22:28
-
-
Save pnomolos/7979177 to your computer and use it in GitHub Desktop.
Recursive chmod for PHP
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
<?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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)