function deldir($dir) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}

//删除文件夹

if(rmdir($dir)) {
return true;
} else {
return false;


}

 

//清空文件夹内的内容保留文件夹

<?php 
function deleteAll($path) {
    $op = dir($path);
    while(false != ($item = $op->read())) {
        if($item == '.' || $item == '..') {
            continue;
        }
        if(is_dir($op->path.'/'.$item)) {
            deleteAll($op->path.'/'.$item);
            rmdir($op->path.'/'.$item);
        } else {
            unlink($op->path.'/'.$item);
        }
    
    }   
}
$path = dirname(__FILE__).'/test';
deleteAll($path)
?>

 

posted on 2017-07-10 16:38  程序小院  阅读(398)  评论(0编辑  收藏  举报