Created
October 22, 2011 02:50
-
-
Save fitorec/1305492 to your computer and use it in GitHub Desktop.
Chmod Recursive for PHP.
This file contains hidden or 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 | |
| /* | |
| * Description: chmod recursive. | |
| * Code-Base: official documentation php.net chmod function | |
| * Link: http://www.php.net/manual/en/function.chmod.php#105570 | |
| * | |
| * Example usage : | |
| * chmod_R( 'mydir', 0666, 0777); | |
| * | |
| */ | |
| function chmod_R($path, $filemode, $dirmode) { | |
| if ( !file_exists($path) ) { | |
| print "Failed file not exists {$path}\n"; | |
| return FALSE; | |
| } | |
| 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 FALSE; | |
| } | |
| $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); | |
| } elseif ( is_file($path) ) { | |
| if (!chmod($path, $filemode)) { | |
| $filemode_str=decoct($filemode); | |
| print "Failed applying filemode {$filemode_str} on file {$path}\n"; | |
| return FALSE; | |
| } | |
| } elseif ( is_link($path) ) { | |
| print "link {$path} is skipped\n"; | |
| return FALSE; | |
| } | |
| } | |
| chmod_R( 'app', 0755, 0755); | |
| ?> | |
| <strong>finish!!!;</strong> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice script. Thanks for sharing. :-)
I would like to share a library I have coded with the same goal.
Example of usage:
Full documentation: https://github.com/MathiasReker/php-chmod