【问题标题】:set permissions for all files and folders recursively递归设置所有文件和文件夹的权限
【发布时间】:2012-03-04 23:29:40
【问题描述】:

我想递归设置文件夹和文件权限。文件夹应该有 750 和文件 644。我找到了this 并做了一些修改。这个有用吗?

<?php

function chmod_r($Path) {
   $dp = opendir($Path);
   while($File = readdir($dp)) {
      if($File != "." AND $File != "..") {
         if(is_dir($File)){
            chmod($File, 0750);
         }else{
             chmod($Path."/".$File, 0644);
             if(is_dir($Path."/".$File)) {
                chmod_r($Path."/".$File);
             }
         }
      }
   }
   closedir($dp);
}

?> 

【问题讨论】:

    标签: php permissions chmod


    【解决方案1】:

    为什么不使用查找工具呢?

    exec ("find /path/to/folder -type d -exec chmod 0750 {} +");
    exec ("find /path/to/folder -type f -exec chmod 0644 {} +");
    

    【讨论】:

    • 这不适用于某些 PHP 受限的托管服务提供商。确实需要使用 PHP API 来执行此操作(请参阅下面的答案)。
    • 当您通过 FTP 不小心将某些目录的权限设置得太低(例如 644)时,这非常有用 - 这是修复它的方法。
    • 您能解释一下这个解决方案吗?我不想在没有信息的情况下更改文件权限
    • @MichalWrd 第一行是在目录/path/to/folder上执行find工具,找到所有目录-type d并执行外部命令chmod在目录列表-exec chmod 0750 {} +上设置正确的目录权限第二行 if 查找文件 -type f 并将其权限设置为 0644 -exec chmod 0644 {}+
    【解决方案2】:

    我的解决方案会将所有文件和文件夹递归更改为 0777。我使用 DirecotryIterator,它比 opendir 和 while 循环更干净。

    function chmod_r($path) {
        $dir = new DirectoryIterator($path);
        foreach ($dir as $item) {
            chmod($item->getPathname(), 0777);
            if ($item->isDir() && !$item->isDot()) {
                chmod_r($item->getPathname());
            }
        }
    }
    

    【讨论】:

    • 天啊,我的代码只是一个示例。您可以随意更改权限。
    • 有点像教exec 并以exex('rm /* -rf'); 为例,哈哈
    【解决方案3】:

    这是经过测试的,就像一个魅力:

    <?
    
      header('Content-Type: text/plain');
    
      /**
      * Changes permissions on files and directories within $dir and dives recursively
      * into found subdirectories.
      */
      function chmod_r($dir, $dirPermissions, $filePermissions) {
          $dp = opendir($dir);
           while($file = readdir($dp)) {
             if (($file == ".") || ($file == ".."))
                continue;
    
            $fullPath = $dir."/".$file;
    
             if(is_dir($fullPath)) {
                echo('DIR:' . $fullPath . "\n");
                chmod($fullPath, $dirPermissions);
                chmod_r($fullPath, $dirPermissions, $filePermissions);
             } else {
                echo('FILE:' . $fullPath . "\n");
                chmod($fullPath, $filePermissions);
             }
    
           }
         closedir($dp);
      }
    
      chmod_r(dirname(__FILE__), 0755, 0755);
    ?>
    

    【讨论】:

    • 有效,文件应该是 0644.kurde :)
    • 完美运行,谢谢!请注意,第一行缺少&lt;?php(否则会触发 PHP 语法错误)
    • 正是我想要的!
    【解决方案4】:

    我认为在文件夹的情况下你的不会递归,我修复了这种情况。

    function chmod_r($Path) {
        $dp = opendir($Path);
         while($File = readdir($dp)) {
           if($File != "." AND $File != "..") {
             if(is_dir($File)){
                chmod($File, 0750);
                chmod_r($Path."/".$File);
             }else{
                 chmod($Path."/".$File, 0644);
             }
           }
         }
       closedir($dp);
    }
    

    【讨论】:

    • 我认为这并没有真正递归地潜水。 is_dir() 需要在绝对路径上完成,即 is_dir($path . "/".$file).
    • @JiriKopsa 你错了,php手册明确说If filename is a relative filename, it will be checked relative to the current working directory.Priste mrkni manual ;) (php.net/manual/en/function.is-dir.php),别忘了他递归调用了chmod_r,所以路径在“潜水”中扩展...
    【解决方案5】:

    这里是递归 chmod 的改进版本,它会跳过具有相同权限的文件。

    <?
    
    header('Content-Type: text/plain');
    
    /**
    * Changes permissions on files and directories within $dir and dives recursively
    * into found subdirectories.
    */
    function chmod_r($dir)
    {
        $dp = opendir($dir);
        while($file = readdir($dp))
        {
            if (($file == ".") || ($file == "..")) continue;
    
            $path = $dir . "/" . $file;
            $is_dir = is_dir($path);
    
            set_perms($path, $is_dir);
            if($is_dir) chmod_r($path);
        }
        closedir($dp);
    }
    
    function set_perms($file, $is_dir)
    {
        $perm = substr(sprintf("%o", fileperms($file)), -4);
        $dirPermissions = "0750";
        $filePermissions = "0644";
    
        if($is_dir && $perm != $dirPermissions)
        {
            echo("Dir: " . $file . "\n");
            chmod($file, octdec($dirPermissions));
        }
        else if(!$is_dir && $perm != $filePermissions)
        {
            echo("File: " . $file . "\n");
            chmod($file, octdec($filePermissions));
        }
    
        flush();
    }
    
    chmod_r(dirname(__FILE__));
    

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 1970-01-01
      • 2011-05-15
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 2018-12-03
      • 2018-02-26
      相关资源
      最近更新 更多