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