Heim  >  Artikel  >  Backend-Entwicklung  >  PHP递归删除目录几个代码实例_PHP教程

PHP递归删除目录几个代码实例_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:31:59809Durchsuche

下面给大家提供几个函数参考。

实例一:

复制代码 代码如下:

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.jbxue.com
                                  if(@unlink($file)){ 
                                         echo "文件$file删除成功。
"; 
                                  }else{ 
                                          echo  "文件$file删除失败!
"; 
                                 } 
                } 
     } 
    if(@rmdir($dir)){ 
           echo "目录$dir删除成功了。
\n"; 
    }else{ 
           echo "目录$dir删除失败!
\n"; 
  } 


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

实例二: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.'
';
        }else{
            echo $tmp_path.'
';
        }

        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.'
';
                    }else{
                        echo $$tmp_path.'/'.$val.'
';
                    }
                }else{
                    delete_dir($tmp_path.'/'.$val);
                }          
            }else{
                if(unlink($tmp_path.'/'.$val)){
                    echo $$tmp_path.'/'.$val.'
';
                }else{
                    echo $$tmp_path.'/'.$val.'
';
                }
            }          
        }else{

            continue;
        }

    }

    
    if(rmdir($tmp_path)){
        echo $tmp_path.'
';
    }else{
        echo $tmp_path.'
';
    }

        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."已被删除
";  
                  //如果不是,删除该文件  
                 if(!unlink($fullpath)) {                 
                 }  
            } else {  
                 //如果是目录,递归本身删除下级目录               
     deldir($fullpath);  
            }  
        }  
   }  
    //关闭目录  
     closedir($dh);  
   //删除目录  
  //if(rmdir($dir)) {  
    //    return true;  
//    } else {  
     //    return false;  
  //  }  
}

实例四:

复制代码 代码如下:

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

}

实例六:

复制代码 代码如下:

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

  }
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/756995.htmlTechArticle下面给大家提供几个函数参考。 实例一: 复制代码 代码如下: ?php function deletedir($dir){ if(!handle=@opendir($dir)){ //检测要打开目录是否存在 di...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn