如何添加子文件夹删除功能



这是一个脚本,它删除目录中的所有文件,但不删除空子文件夹和其中包含文件的子文件夹。

<?Php
$dir='directory name here'; // directory name 
$ar=scandir($dir); 
$box=$_POST['box'];  // Receive the file list from form
// Looping through the list of selected files ///
while (list ($key,$val) = @each ($box)) {
$path=$dir  ."/".$val;
if(unlink($path)) echo "Deleted file ";
echo "$val,";
}
echo "<hr>";
/// displaying the file names with checkbox and form ////
echo "<form method=post name='f1' action=''>";
while (list ($key, $val) = each ($ar)) {
if(strlen($val)>3){
echo "<input class=roundedOne id=roundedOne type=checkbox name=box[] value='$val'>$val<br>";
}
}
echo "<input class=button1 type=submit value='Delete'></form>";
?>

此函数将删除子文件夹及其文件:

function removeDir($dir) {
    if (is_dir($dir)) {
        $objects = scandir($dir);
        foreach ($objects as $object) {
            if ($object !== '.' && $object !== '..') {
                if (filetype($dir.'/'.$object) === "dir") {
                    removeDir($dir . '/' . $object);
                }
                else {
                    unlink($dir.'/'.$object);
                }
            }
        }
        reset($objects);
        unlink($dir);
    }
}

您可以通过执行removeDir($dir);

来使用它

最新更新