• <i id='cLApW'><tr id='cLApW'><dt id='cLApW'><q id='cLApW'><span id='cLApW'><b id='cLApW'><form id='cLApW'><ins id='cLApW'></ins><ul id='cLApW'></ul><sub id='cLApW'></sub></form><legend id='cLApW'></legend><bdo id='cLApW'><pre id='cLApW'><center id='cLApW'></center></pre></bdo></b><th id='cLApW'></th></span></q></dt></tr></i><div id='cLApW'><tfoot id='cLApW'></tfoot><dl id='cLApW'><fieldset id='cLApW'></fieldset></dl></div>

      <bdo id='cLApW'></bdo><ul id='cLApW'></ul>
      <legend id='cLApW'><style id='cLApW'><dir id='cLApW'><q id='cLApW'></q></dir></style></legend>

      <small id='cLApW'></small><noframes id='cLApW'>

        <tfoot id='cLApW'></tfoot>

        使用 PHP 删除空子文件夹

        Remove empty subfolders with PHP(使用 PHP 删除空子文件夹)
        <legend id='dN4Zf'><style id='dN4Zf'><dir id='dN4Zf'><q id='dN4Zf'></q></dir></style></legend>

        <small id='dN4Zf'></small><noframes id='dN4Zf'>

          <tbody id='dN4Zf'></tbody>

        <i id='dN4Zf'><tr id='dN4Zf'><dt id='dN4Zf'><q id='dN4Zf'><span id='dN4Zf'><b id='dN4Zf'><form id='dN4Zf'><ins id='dN4Zf'></ins><ul id='dN4Zf'></ul><sub id='dN4Zf'></sub></form><legend id='dN4Zf'></legend><bdo id='dN4Zf'><pre id='dN4Zf'><center id='dN4Zf'></center></pre></bdo></b><th id='dN4Zf'></th></span></q></dt></tr></i><div id='dN4Zf'><tfoot id='dN4Zf'></tfoot><dl id='dN4Zf'><fieldset id='dN4Zf'></fieldset></dl></div>
      1. <tfoot id='dN4Zf'></tfoot>
                • <bdo id='dN4Zf'></bdo><ul id='dN4Zf'></ul>
                • 本文介绍了使用 PHP 删除空子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个 PHP 函数,该函数将递归删除所有不包含从给定绝对路径开始的文件的子文件夹.

                  I am working on a PHP function that will recursively remove all sub-folders that contain no files starting from a given absolute path.

                  这是目前开发的代码:

                  function RemoveEmptySubFolders($starting_from_path) {
                  
                      // Returns true if the folder contains no files
                      function IsEmptyFolder($folder) {
                          return (count(array_diff(glob($folder.DIRECTORY_SEPARATOR."*"), Array(".", ".."))) == 0);
                      }
                  
                      // Cycles thorugh the subfolders of $from_path and
                      // returns true if at least one empty folder has been removed
                      function DoRemoveEmptyFolders($from_path) {
                          if(IsEmptyFolder($from_path)) {
                              rmdir($from_path);
                              return true;
                          }
                          else {
                              $Dirs = glob($from_path.DIRECTORY_SEPARATOR."*", GLOB_ONLYDIR);
                              $ret = false;
                              foreach($Dirs as $path) {
                                  $res = DoRemoveEmptyFolders($path);
                                  $ret = $ret ? $ret : $res;
                              }
                              return $ret;
                          }
                      }
                  
                      while (DoRemoveEmptyFolders($starting_from_path)) {}
                  }
                  

                  根据我的测试,此功能有效,但我很高兴看到任何改进代码性能的想法.

                  As per my tests this function works, though I would be very delighted to see any ideas for better performing code.

                  推荐答案

                  如果您在空文件夹内有空文件夹内有空文件夹,则需要在所有文件夹中循环 3 次.所有这一切,因为您首先要进行广度测试 - 在测试其子项之前测试文件夹.相反,您应该在测试父文件夹是否为空之前进入子文件夹,这样一次就足够了.

                  If you have empty folder within empty folder within empty folder, you'll need to loop through ALL folders three times. All this, because you go breadth first - test folder BEFORE testing its children. Instead, you should go into child folders before testing if parent is empty, this way one pass will be sufficient.

                  function RemoveEmptySubFolders($path)
                  {
                    $empty=true;
                    foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file)
                    {
                       if (is_dir($file))
                       {
                          if (!RemoveEmptySubFolders($file)) $empty=false;
                       }
                       else
                       {
                          $empty=false;
                       }
                    }
                    if ($empty) rmdir($path);
                    return $empty;
                  }
                  

                  顺便说一下, glob 不会返回.和...条目.

                  By the way, glob does not return . and .. entries.

                  更短的版本:

                  function RemoveEmptySubFolders($path)
                  {
                    $empty=true;
                    foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file)
                    {
                       $empty &= is_dir($file) && RemoveEmptySubFolders($file);
                    }
                    return $empty && rmdir($path);
                  }
                  

                  这篇关于使用 PHP 删除空子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <i id='YgAuu'><tr id='YgAuu'><dt id='YgAuu'><q id='YgAuu'><span id='YgAuu'><b id='YgAuu'><form id='YgAuu'><ins id='YgAuu'></ins><ul id='YgAuu'></ul><sub id='YgAuu'></sub></form><legend id='YgAuu'></legend><bdo id='YgAuu'><pre id='YgAuu'><center id='YgAuu'></center></pre></bdo></b><th id='YgAuu'></th></span></q></dt></tr></i><div id='YgAuu'><tfoot id='YgAuu'></tfoot><dl id='YgAuu'><fieldset id='YgAuu'></fieldset></dl></div>

                  <small id='YgAuu'></small><noframes id='YgAuu'>

                        • <bdo id='YgAuu'></bdo><ul id='YgAuu'></ul>
                          <legend id='YgAuu'><style id='YgAuu'><dir id='YgAuu'><q id='YgAuu'></q></dir></style></legend>
                            <tbody id='YgAuu'></tbody>
                          <tfoot id='YgAuu'></tfoot>