note 105570 added to function.chmod
| From: | h dot kuppens at cs dot ru dot nl | Date: | Mon, 29 Aug 2011 09:51:57 +0000 |
| Subject: | note 105570 added to function.chmod | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182794@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;
}
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 131.174.142.56
----
Manual Page -- http://www.php.net/manual/en/function.chmod.php
Edit -- https://master.php.net/note/edit/105570
Del: integrated -- https://master.php.net/note/delete/105570/integrated
Del: useless -- https://master.php.net/note/delete/105570/useless
Del: bad code -- https://master.php.net/note/delete/105570/bad+code
Del: spam -- https://master.php.net/note/delete/105570/spam
Del: non-english -- https://master.php.net/note/delete/105570/non-english
Del: in docs -- https://master.php.net/note/delete/105570/in+docs
Del: other reasons-- https://master.php.net/note/delete/105570
Reject -- https://master.php.net/note/reject/105570
Search -- https://master.php.net/manage/user-notes.php