php删除特定文件夹及其所有内容

标签 php recursion directory

我正在使用 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;
}

?>

最佳答案

<?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");
?>

关于php删除特定文件夹及其所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12621753/

相关文章:

php - 如何将 SQL 查询 ORDER BY ID DESC 生成为超过 1 个 div

php - 使用模块中的 hook_theme 将变量传递给 twig

Python更改用户目录

c++ - 递归函数可以写入 C++ 中的文件吗?

python - 为什么我不能导入 folium python 包?

flash - java中如何确定包名,为什么 "com"作为root?

php - 提交表单和使用值 (JavaScript)

php - 完全合格的接口(interface)名称

C++ 模板递归 - 如何解决?

java - 如何在 2 次迭代后停止此递归方法