Recursively chmod / chown / chgrp all files and folders in a directory

I am working on a site that builds other sites. Some, if I use copy () to create files and directories, sometimes I create XML files in php and using DOMDocument :: save to save them. The end result is a root folder with all kinds of confused permissions. I do file and folder modding when I go, which words are to some extent, but I especially have problems when it comes to using copy() .

(This is where I am http://pastebin.com/SBE8vtFX , attn: function modPath($path) )

I want to use a different approach and recursively chmod / chown / chgrp all the files and folders in my document root directory for my specifications right away.

Take, for example, the root of a document

 /home/mysite/public_html 

and inside public_html i have

 -rwxrwxrwx 1 mysite mysite 348 Aug 31 10:49 index.php d--------x 5 root root 4096 Aug 30 10:21 folder1 drwxrwxrwx 2 mysite mysite 4096 Aug 30 09:41 folder2 

My question is:

How can I mod all the files in the specified directory at once? I want to distinguish between different chmod settings between directories and folders. It must be a PHP solution.

This is how much I can get

 <?php function modAll($root) { $aPath = explode("/", $root); $user = $aPath[2]; /* Some sort of looping through $root */ { $mod = (is_dir($thisfileorfolder) ? 0755 : 0644); chmod($thisfileorfolder, $mod); chown($thisfileorfolder, $user); chgrp($thisfileorfolder, $user); } } ?> 
+6
source share
3 answers

This should be helpful. EDITED: fixed some syntax errors

  function fsmodify($obj) { $chunks = explode('/', $obj); chmod($obj, is_dir($obj) ? 0755 : 0644); chown($obj, $chunks[2]); chgrp($obj, $chunks[2]); } function fsmodifyr($dir) { if($objs = glob($dir."/*")) { foreach($objs as $obj) { fsmodify($obj); if(is_dir($obj)) fsmodifyr($obj); } } return fsmodify($dir); } 
+11
source

You can make a system call

 system("/bin/chmod -R $mod $root"); system("/bin/chown -R $user $root"); system("/bin/chgrp -R $user $root"); 

of course you use escapeshellarg () or escapeshellcmd () to avoid executing arbitrary commands

+8
source
 system("/bin/chmod -R $mod $root"); system("/usr/bin/find -type d $root -print0 | xargs -0 | /bin/chmod $moddir"); system("/bin/chown -R $user $root"); system("/bin/chgrp -R $user $root"); 

Invalid 493 mode means you passed your mode as decimal. First convert to octal string.

0
source

Source: https://habr.com/ru/post/896340/


All Articles