温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在PHP中使用递归删除目录

发布时间:2021-03-09 17:25:45 来源:亿速云 阅读:97 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关怎么在PHP中使用递归删除目录,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

实例一:

复制代码 代码如下:


<?php 
function deletedir($dir){ 
      if(!handle=@opendir($dir)){     //检测要打开目录是否存在 
               die("没有该目录"); 
      } 
     while(false !==($file=readdir($handle))){ 
               if($file!=="."&&$file!==".."){       //排除当前目录与父级目录 
                            $file=$dir .DIRECTORY_SEPARATOR. $file; 
                            if(is_dir($file)){ 
                                  deletedir($file); 
                            }else{  // www.jb51.net
                                  if(@unlink($file)){ 
                                         echo "文件<b>$file</b>删除成功。<br>"; 
                                  }else{ 
                                          echo  "文件<b>$file</b>删除失败!<br>"; 
                                 } 
                } 
     } 
    if(@rmdir($dir)){ 
           echo "目录<b>$dir</b>删除成功了。<br>\n"; 
    }else{ 
           echo "目录<b>$dir</b>删除失败!<br>\n"; 
  } 


//测试程序 
$dir="/var/www/test"; 
deletedir($dir); 
?> 

实例二:Php递归删除目录方法(兼容中文目录和文件)

复制代码 代码如下:


<?php
/*
本函数支持中文删除目录,由于我只测试了window下删除,linux未测试,如有问题可留言或者自己稍加修改即可。
如有错误欢迎大家指正,共同学习
*/
header("Content-type: text/html; charset=gb2312");

function delete_dir($tmp_path){

 
    if(!is_writable($tmp_path) && is_dir($tmp_path)){
        chmod($tmp_path,0777);
    }

    $encode = mb_detect_encoding($tmp_path, array('UTF-8','GB2312','ASCII','GBK'));
    $tmp_path = iconv($encode,'gb2312',$tmp_path);

 
    $handle_object = scandir($tmp_path);

 
    if(count(scandir($tmp_path))==2){

        if(rmdir($tmp_path)){
            echo $tmp_path.'<br />';
        }else{
            echo $tmp_path.'<br />';
        }

        return ;

    }

    foreach($handle_object as $val){       

        if($val!='..' && $val!='.' && $val!=''){

            if(filetype($tmp_path.'/'.$val)=='dir'){

                if(count(scandir($tmp_path.'/'.$val))==2){
                    if(rmdir($tmp_path.'/'.$val)){
                        echo $$tmp_path.'/'.$val.'<br />';
                    }else{
                        echo $$tmp_path.'/'.$val.'<br />';
                    }
                }else{
                    delete_dir($tmp_path.'/'.$val);
                }          
            }else{
                if(unlink($tmp_path.'/'.$val)){
                    echo $$tmp_path.'/'.$val.'<br />';
                }else{
                    echo $$tmp_path.'/'.$val.'<br />';
                }
            }          
        }else{

            continue;
        }

    }

    
    if(rmdir($tmp_path)){
        echo $tmp_path.'<br />';
    }else{
        echo $tmp_path.'<br />';
    }

        return ;

    
}

delete_dir('D:/AppServ/www/testing/哈哈');

?>

实例三:参数$dir文件名例子:admin/runtime 这样的

复制代码 代码如下:


//删除目录及所包含文件函数  
function deldir($dir) {  
     //打开文件目录  
     $dh = opendir($dir);  
     //循环读取文件  
     while ($file = readdir($dh)) {  
         if($file != '.' && $file != '..') {  
             $fullpath = $dir . '/' . $file;  

    //判断是否为目录  
             if(!is_dir($fullpath)) {  
       echo $fullpath."已被删除<br>";  
                  //如果不是,删除该文件  
                 if(!unlink($fullpath)) {                 
                 }  
            } else {  
                 //如果是目录,递归本身删除下级目录               
     deldir($fullpath);  
            }  
        }  
   }  
    //关闭目录  
     closedir($dh);  
   //删除目录  
  //if(rmdir($dir)) {  
    //    return true;  
//    } else {  
     //    return false;  
  //  }  
}

实例四:

复制代码 代码如下:


<?php 
function deldir($dirname){ 
    if(file_exists($dirname)){//首先判断目录是否有效 
        $dir = opendir($dirname);//用opendir打开目录 
        while($filename = readdir($dir)){//使用readdir循环读取目录里的内容 
         if($filename != "." && $filename != ".."){//排除"."和".."这两个特殊的目录 
            $file = $dirname."/".$filename; 
            if(is_dir($file)){//判断是否是目录,如果是则调用自身 
                deldir($file); //使用递归删除子目录   
            }else{ 
              @unlink($file);//删除文件 
            } 
          } 
        } 
            closedir($dir);//关闭文件操作句柄 
            rmdir($dirname);//删除目录 
    } 

?>

实例五:

复制代码 代码如下:


/**
 * 删除非空目录
 * @method rrmdir
 */
function rrmdir ($dir) {

    if (is_dir($dir)) {

        $fs = array_slice(scandir($dir), 2);
        foreach ($fs as $f) {
            $path = $dir . '/' . $f;
            is_dir($path) ? rrmdir($path) : unlink($path);
        }
        reset($fs);
        return rmdir($dir);
    }

}

实例六:

复制代码 代码如下:


<?php
function del_dir( $dir )
{
  if ( $handle = opendir( $dir ) )
  {
    while ( false !== ( $item = readdir( $handle ) ) )
    {
      if ( $item != "." && $item != ".." )
      {
        if ( is_dir( "$dir/$item" ) )
        {
          del_dir( "$dir/$item" );
        }
        else
        {
          unlink( "$dir/$item" ) ;
        }
      }
    }
    closedir( $handle );
    rmdir( $dir ) ;

  }
}
?>

上述就是小编为大家分享的怎么在PHP中使用递归删除目录了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI