php 递归 chmod/chown/chgrp 目录中的所有文件和文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7261566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:20:34  来源:igfitidea点击:

Recursively chmod/chown/chgrp all files and folder within a directory

phprecursioncronfile-permissionschmod

提问by Steve Robbins

I am working on a site which builds other sites. Some if it I use copy()to create the files and directories, other times I'm building XML files in php and using DOMDocument::saveto save them. The end result is a root folder with all sorts of messed up permissions. I've beening modding files and folders as I go, which words to some extent, but I'm particularly having trouble when it comes to using copy().

我正在一个建立其他网站的网站上工作。有些时候我使用copy()创建文件和目录,其他时候我在 php 中构建 XML 文件并使用DOMDocument::save来保存它们。最终结果是一个具有各种混乱权限的根文件夹。我一直在修改文件和文件夹,这在某种程度上说,但在使用copy().

(This is where I'm at so far http://pastebin.com/SBE8vtFX, attn: function modPath($path))

(这是我在哪里至今http://pastebin.com/SBE8vtFX,经办人:function modPath($path)

I want to take a different approach and recursively chmod/chown/chgrpall the files and folders within my document root to my specifications at once.

我要采取不同的方法和递归CHMOD / CHOWN / chgrp命令所有的我的文档根目录中的文件和文件夹到我的规格一次

Take for example the document root

以文档根目录为例

/home/mysite/public_html

and within public_htmlI have

并在public_html

-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:

我的问题:

How can I mod allfiles within a specified directory at once? I want to differentiate different chmod settings between directories and folders as well. This needs to be a PHP solution.

如何一次修改指定目录中的所有文件?我也想区分目录和文件夹之间的不同 chmod 设置。这需要是一个 PHP 解决方案。

This is as far as 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);
        }
    }

?>

回答by Felipe Buccioni

This should be helpful. EDITED: some syntax errors corrected

这应该会有所帮助。已编辑:更正了一些语法错误

    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);
    }   

回答by rouzier

You can perform a systemcall

您可以执行系统调用

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() in order to avoid executing arbitrary commands

当然你使用escapeshellarg()或escapeshellcmd()是为了避免执行任意命令

回答by Joshua

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 mode 493 means you passed your mode as decimal. Convert to octal string first.

无效模式 493 表示您以十进制形式传递了您的模式。首先转换为八进制字符串。