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

时间:2016-10-25 21:35:59

标签: php subdirectory

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

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

1 个答案:

答案 0 :(得分:0)

此功能将删除子文件夹及其文件:

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

来使用它