note 105570 modified in function.chmod by danbrown

From: Date: Mon, 29 Aug 2011 12:46:29 +0000
Subject: note 105570 modified in function.chmod by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182802@lists.php.net to get a copy of this message
For recursive chmod'ing both files and directories in one step you can use the function below. Note that this function has one argument for directory permissions and one for file permissions. In this way you can apply the 'x' permission on directories, and skip it on directories. This function also skips links. Example usage : chmod_R( 'mydir', 0666, 0777); Makes all files and directories within mydir/ directory accessible for everyone, but doesn't make the files executable. I guess for very deeply nested directories the recursion with php can become problematic, but I can be wrong because I never investigated this. <?php function chmod_R($path, $filemode, $dirmode) { if (is_dir($path) ) { if (!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 (!chmod($path, $filemode)) { $filemode_str=decoct($filemode); print "Failed applying filemode '$filemode_str' on file '$path'\n"; return; } } } ?> --was-- For recursive chmod'ing both files and directories in one step you can use the function below. Note that this function has one argument for directory permissions and one for file permissions. In this way you can apply the 'x' permission on directories, and skip it on directories. This function also skips links. Example usage : chmod_R( 'mydir', 0666, 0777); Makes all files and directories within mydir/ directory accessible for everyone, but doesn't make the files executable. I guess for very deeply nested directories the recursion with php can become problematic, but I can be wrong because I never investigated this. <?php function chmod_R($path, $filemode, $dirmode) { if (is_dir($path) ) { if (!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 (!chmod($path, $filemode)) { $filemode_str=decoct($filemode); print "Failed applying filemode '$filemode_str' on file '$path'\n"; return; } } } http://php.net/manual/en/function.chmod.php

« previous php.notes (#182802) next »