【问题标题】:Using scandir() to find folders in a directory (PHP)使用 scandir() 在目录中查找文件夹 (PHP)
【发布时间】:2010-10-11 03:25:03
【问题描述】:

我正在使用这段代码:

$target = 'extracted/' . $name[0];  
$scan = scandir($target);

扫描用于 zip 上传的文件夹的目录。我希望能够找到我的$target 文件夹中的所有文件夹,这样我就可以删除它们及其内容,只留下$target 目录中的文件。

返回文件夹的内容后,我不知道如何区分文件夹和文件才能删除文件夹。

另外,有人告诉我rmdir() 函数不能删除其中包含内容的文件夹,有什么办法可以解决这个问题吗?

谢谢,本。

【问题讨论】:

    标签: php directory


    【解决方案1】:

    要确定您是否有文件夹或文件,请使用函数is_dir()is_file()

    例如:

    $path = '提取/' 。 $名称[0]; $results = scandir($path); foreach ($results 作为 $result) { if ($result === '.' 或 $result === '..') 继续; if (is_dir($path . '/' . $result)) { //使用if目录的代码 } }

    【讨论】:

    • 这太棒了,完美运行!不幸的是,我不明白这部分“if ($result === '.' or $result === '..') continue;”的作用,你介意解释一下吗?再次感谢。
    • @Ben McRae:这是因为 scandir 返回结果“。”和“..”作为数组的一部分,在大多数情况下,您希望忽略这些结果,这就是为什么我将其作为 foreach 的一部分
    • 只是添加一些附加信息:“。”代表当前目录,而“..”代表文件夹的父目录。
    【解决方案2】:

    最好用DirectoryIterator

    $path = 'extracted'; // '.' for current
    foreach (new DirectoryIterator($path) as $file) {
        if ($file->isDot()) continue;
    
        if ($file->isDir()) {
            print $file->getFilename() . '<br />';
        }
    }
    

    【讨论】:

      【解决方案3】:

      首先,rmdir() 无法删除包含内容的文件夹。如果安全模式被禁用,您可以使用以下方法。

      exec("rm -rf folder/"); 
      

      还可以查看 is_dir()/is_file() 甚至更好的 PHP SPL

      【讨论】:

      • 您的删除功能很有效! rm -rf 实际上是做什么的?谢谢
      • 对于任何想知道的人,rm -rf 使用选项-r(递归 - 递归删除目标文件夹中的文件和文件夹)在目标目录(在本例中为文件夹/)运行 rm 函数) 和-f (强制 - 不要提示错误等并忽略不存在的文件)。基本上,它确保在删除目标时处理目标文件夹中的所有文件和文件夹。一般来说,如果您在包含其他内容的文件夹上尝试rm,它只会抛出一个错误,说明它不是空的。 more details here
      【解决方案4】:
      $directories = scandir('images');
      foreach($directories as $directory){
          if($directory=='.' or $directory=='..' ){
              echo 'dot';
          }else{
                  if(is_dir($directory)){
                        echo $directory .'<br />';
                  }
          }
      } 
      

      一个更简单,也许更快的版本

      【讨论】:

      • 你没有过滤 else 中的结果是否是目录,让我编辑和更新它
      【解决方案5】:

      scandir 会扫描整个目录,可以手动过滤。

      但如果你像我一样懒惰,那就用 glob

      $scan = glob($target."/*",GLOB_ONLYDIR);
      

      它会输出一个包含目标所有目录的数组。

      【讨论】:

      • 真的很管用。我喜欢它。并且还使用了它。谢谢
      【解决方案6】:

      获取所有子、子文件夹中的所有文件

      function myfunction($dir){
      
      foreach ($dir as $dirname => $file) {
      
      if(is_dir($file) &&  $file != '.' &&  $file != '..' ) { 
      
           // echo $file;
            $newDir = scandir($file);
            myfunction($newDir);
      
          }elseif($file !='.' && $file != '..'){
      
              echo "<br>File name is ---";
              echo  $file;
          }
      
      } // end foreach
      
      
      }  //function ends 
      
      $dirpass = scandir($mypath3); // set directory
      echo myfunction($dirpass); //     pass directory
      

      我们会得到如下结果(请忽略文件名)

      File name is ----->index.PHP
      File name is -----> 100000045   Invoices   Sales   Magento Admin.png
      File name is -----> 100000142   Orders   Sales   Magento Admin(1).png
      File name is -----> 100000142   Orders   Sales   Magento Admin.png
      File name is ----->hrc-siberian-tiger-2-jpg_21253111.jpg
      File name is ----->images (3rd copy).jpeg
      File name is ----->images (4th copy).jpeg
      File name is ----->images (5th copy).jpeg
      File name is ----->images (another copy).jpeg
      File name is ----->images (copy).jpeg
      File name is ----->images.jpeg
      File name is ----->JPEG_example_JPG_RIP_100.jpg
      File name is ----->preload
      File name is ----->Stonehenge (3rd copy).jpg
      File name is ----->Stonehenge (4th copy).jpg
      File name is ----->Stonehenge (5th copy).jpg
      File name is ----->Stonehenge (another copy).jpg
      File name is ----->Stonehenge (copy).jpg
      File name is ----->Stonehenge.jpg
      

      【讨论】:

        【解决方案7】:

        如果项目位于该目录中,您还想删除它们。 rmdir 不允许您删除包含文件的目录。但是有一个简单的解决方案。

        array_map('unlink', glob($target.'/*/*'));
        array_map('rmdir',glob($target."/*",GLOB_ONLYDIR));
        

        首先它将取消链接所有子目录中的所有文件。
        其次,它将删除所有目录,因为它们不包含文件。

        如果您有子子目录,那么您应该添加另外 2 行,如下所示:

        array_map('unlink', glob($target.'/*/*/*')); //remove sub-sub-files
        array_map('rmdir',glob($target."/*/*",GLOB_ONLYDIR)); //remove sub-sub-directories
        
        array_map('unlink', glob($target.'/*/*')); //remove sub-files
        array_map('rmdir',glob($target."/*",GLOB_ONLYDIR)); //remove sub-directories
        

        【讨论】:

          【解决方案8】:

          快速而肮脏的方式:

          $folders = glob("<path>/*", GLOB_ONLYDIR);
          

          一个更通用和面向对象的解决方案,灵感来自早期使用 DirectoryIterator 的答案,但更简洁和通用:

              $path = '<path>';
              $folders = [];
          
              foreach (new \DirectoryIterator($path) as $file)
              {
                  if (!$file->isDot() && $file->isDir())
                  {
                      $folders[] = $file;
                  }
              }
          

          【讨论】:

            【解决方案9】:

            这是我主要用于解析档案和目录的一个函数

            function scan_full_dir($dir,$child=false){
                $dir_content_list = scandir($dir);
                foreach($dir_content_list as $value) 
                { 
                    if($value === '.' || $value === '..') {continue;} 
                    // check if we have file 
                     if(is_file($dir.'/'.$value)) {
                        echo '<br> File Name --> '.$value;
                        continue; 
                     }
                    // check if we have directory
                    if (is_dir($dir.'/'.$value)) {
                        if(!$child){ 
                            echo '<br> parent --> '.$value;
                        }else{
                            echo '<br> child of '.$child.' --> '.$value;    
                        }
                        scan_full_dir($dir.'/'.$value,$value);
                    }       
                }   
            }
            

            输出样本

            parent --> fonts
            File Name --> standard
            parent --> steps
            child of steps --> pcb
            File Name --> .attrlist.sum
            File Name --> .profile.sum
            File Name --> .stephdr.sum
            File Name --> attrlist
            child of pcb --> netlists
            child of netlists --> cadnet
            File Name --> .netlist.sum
            File Name --> netlist
            File Name --> profile
            File Name --> stephdr
            

            // 扫描目录

            scan_full_dir('path/of/myfolder');
            

            // 无需打开即可扫描存档,支持的格式:gz、zip、tar 和 bz 文件。

            scan_full_dir('phar://uploads/youarchive.zip);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-11-12
              • 1970-01-01
              • 2016-05-15
              • 1970-01-01
              • 2017-03-16
              • 1970-01-01
              • 2022-09-27
              相关资源
              最近更新 更多