【问题标题】:php deleting a specific folder and all its contentsphp删除特定文件夹及其所有内容
【发布时间】:2012-09-19 06:15:48
【问题描述】:

我正在使用 php 删除包含已删除帖子图像的文件夹。我正在使用我在网上找到的下面的代码并且做得很好。

我想知道当一个文件夹中有其他文件夹时,如何只删除其中的特定文件夹。

当我使用下面的代码时,怎么可能做到这一点? 使用:/dev/images/norman/8 -> 不会删除文件夹 8 使用:/dev/images/norman/ -> 将删除所有文件夹

Eg:
/dev/images/norman/8 -> I need to delete only this folder
/dev/images/norman/9
/dev/images/norman/10
/dev/images/norman/11
/dev/images/norman/12

<?php
$path = $_SERVER['DOCUMENT_ROOT'].'/dev/images/norman/8';

emptyDir($path);

function emptyDir($path) { 

    // INITIALIZE THE DEBUG STRING
    $debugStr  = '';
    $debugStr .= "Deleting Contents Of: $path<br /><br />";

    // PARSE THE FOLDER
    if ($handle = opendir($path)) {

        while (false !== ($file = readdir($handle))) {

            if ($file != "." && $file != "..") {

                // IF IT"S A FILE THEN DELETE IT
                if(is_file($path."/".$file)) {

                    if(unlink($path."/".$file)) {
                    $debugStr .= "Deleted File: ".$file."<br />";   
                    }

                } else {

                    // IT IS A DIRECTORY
                    // CRAWL THROUGH THE DIRECTORY AND DELETE IT'S CONTENTS

                    if($handle2 = opendir($path."/".$file)) {

                        while (false !== ($file2 = readdir($handle2))) {

                            if ($file2 != "." && $file2 != "..") {
                                if(unlink($path."/".$file."/".$file2)) {
                                $debugStr .= "Deleted File: $file/$file2<br />";    
                                }
                            }

                        }

                    }

                    if(rmdir($path."/".$file)) {
                    $debugStr .= "Directory: ".$file."<br />";  
                    }

                }

            }

        }

    }
    echo $debugStr;
}

?>

【问题讨论】:

  • @FirmView 这是删除文件夹 8 下的所有内容,但不删除文件夹 8
  • 你试过我发布的解决方案了吗?
  • 是的,我运行了它,它运行得非常好。等一下。
  • 如果我的答案是正确的,那么你可以检查它作为正确答案

标签: php recursion directory


【解决方案1】:

您想了解rmdir

if(is_file($path."/".$file)) {

    if(unlink($path."/".$file)) {
    $debugStr .= "Deleted File: ".$file."<br />";   
    }

} else {

    if(rmdir($path."/".$file)) {
        $debugStr .= "Deleted Directory: ".$file."<br />";   
    }

}

编辑:rmdir 只能处理空目录,您可以使用 rmdir 页面 cmets 中报告的此解决方案:

function rrmdir($dir) {
    foreach(glob($dir . '/*') as $file) {
        if(is_dir($file))
            rrmdir($file);
        else
            unlink($file);
    }
    rmdir($dir);
}

它只是递归删除$dir 中的所有内容,然后删除目录本身。

【讨论】:

  • rmdir 只能删除空目录。
  • @S3Mi 我的错,我会提供递归解决方案。
【解决方案2】:

您可以使用系统命令,例如。 exec("rm -rf {$dirPath}"); 或者如果你想用 PHP 来做,你必须递归,循环不会做对。

public function deleteDir($path) {

    if ($handle = opendir($path)) {

        while (false !== ($file = readdir($handle))) {

            if ($file != "." && $file != "..") {

                // IF IT"S A FILE THEN DELETE IT
                if(is_file($path."/".$file)) {

                    if(unlink($path."/".$file)) {
                        $debugStr .= "Deleted File: ".$file."<br />";
                    }

                } else {
                    deleteDir($path."/".$file."/");
                    rmdir($path."/".$file);
                }
            }
        }
    }
}

当我使用下面的代码时,怎么可能做到这一点?使用: /dev/images/norman/8 -> 不会删除文件夹 8 使用: /dev/images/norman/ -> 将删除所有文件夹

我认为您的问题是您在“/dev/images/norman/8”的末尾缺少“/”

【讨论】:

    【解决方案3】:
    <?php
    
    delete_directory($dirname) {
       if (is_dir($dirname))
          $dir_handle = opendir($dirname);
       if (!$dir_handle)
          return false;
       while($file = readdir($dir_handle)) {
          if ($file != "." && $file != "..") {
             if (!is_dir($dirname."/".$file))
                unlink($dirname."/".$file);
             else
                delete_directory($dirname.'/'.$file);    
          }
       }
       closedir($dir_handle);
       rmdir($dirname);
       return true;
     }
    ?>
    

    如果您使用的是 5.1 及更高版本,

    <?php
    function deleteDir($dir) {
       $iterator = new RecursiveDirectoryIterator($dir);
       foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) 
       {
          if ($file->isDir()) {
             rmdir($file->getPathname());
          } else {
             unlink($file->getPathname());
          }
       }
       rmdir($dir);
    }
    
    deleteDir("temporary");
    ?>
    

    【讨论】:

    • 首先将删除文件夹的所有内容。最后,该文件夹将被删除。
    【解决方案4】:

    我在你的函数中添加了一个$exclude 参数,这个参数是一个数组,其中包含你要排除的目录名称,如下所示:

    $path = $_SERVER['DOCUMENT_ROOT'].'/dev/images/norman/';
    
    emptyDir($path); //will delete all under /norman/
    emptyDir($path, array('8')); //will delete all under /norman/ except dir 8
    emptyDir($path, array('8','10')); //will delete all under /norman/ except dir 8 and 10
    
    function emptyDir($path,$exclude=false) { 
    
        // INITIALIZE THE DEBUG STRING
        $debugStr  = '';
        $debugStr .= "Deleting Contents Of: $path<br /><br />";
        if (!$exclude) {
           $exclude = array();
        }
    
        // PARSE THE FOLDER
        if ($handle = opendir($path)) {
    
            while (false !== ($file = readdir($handle))) {
    
                if ($file != "." && $file != "..") {
    
                    // IF IT"S A FILE THEN DELETE IT
                    if(is_file($path."/".$file)) {
    
                        if(unlink($path."/".$file)) {
                        $debugStr .= "Deleted File: ".$file."<br />";   
                        }
    
                    } else if (!in_array($file, $exclude)) {
    
                        // IT IS A DIRECTORY
                        // CRAWL THROUGH THE DIRECTORY AND DELETE IT'S CONTENTS
    
                        if($handle2 = opendir($path."/".$file)) {
    
                            while (false !== ($file2 = readdir($handle2))) {
    
                                if ($file2 != "." && $file2 != "..") {
                                    if(unlink($path."/".$file."/".$file2)) {
                                    $debugStr .= "Deleted File: $file/$file2<br />";    
                                    }
                                }
    
                            }
    
                        }
    
                        if(rmdir($path."/".$file)) {
                        $debugStr .= "Directory: ".$file."<br />";  
                        }
    
                    }
    
                }
    
            }
    
        }
        echo $debugStr;
    }
    

    【讨论】:

      【解决方案5】:
      $path='./ggg';
      rrmdir($path);
      function rrmdir($dir) {
      if (is_dir($dir)) {
       $objects = scandir($dir);
       foreach ($objects as $object) {
         if ($object != "." && $object != "..") {
           if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
         }
       }
       reset($objects);
       rmdir($dir);
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-15
        相关资源
        最近更新 更多