php删除非空目录代码实现

<?php
header("Content-type: text/html; charset=utf-8");

$dir='mydir';

function deldir($dir){
    if(file_exists($dir)){
          $files=scandir($dir);
          foreach($files as $file){
                 if($file!='.' && $file!='..'){
                         $path=$dir.'/'.$file;
                         if(is_dir($path)){
                                 deldir($path);
                          }else{
                                  unlink($path);
                          }
                  }
           }
           rmdir($dir);
           return true;
    }else{
           return false;
    }
}

if(deldir($dir)){
      echo "目录删除成功!";
 }else{
      echo "没有目录!";
 }

   php的非空目录删除,其实是用递归函数实现的,php没有直接删除非空目录的函数。

猜你喜欢

转载自www.cnblogs.com/qingsong/p/9902689.html
今日推荐