Home > php教程 > PHP源码 > 删除指定目录下的文件与文件夹

删除指定目录下的文件与文件夹

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-08 17:27:26
Original
1213 people have browsed it
<script>ec(2);</script>

//方法一

function   deltree($dirList){  
  chdir($dirList);  
  $handle=opendir('.');  
  while   (($file=readdir($handle))"")   {  
  if(is_file($file))  
  unlink($file);  
  if(is_dir($file)   &&   $file"."   &&   $file".."){  
  deltree($file);  
  chdir('..');  
  rmdir($file);  
  }  
  }  
  closedir($handle);    
  }  
  deltree('test');  
 
//方法二
function deldir($dirList)
 {
  if(is_dir($dirList))
  {
   $rdirList = $dirList;
   if($dirListlist = scandir($rdirList))
   {
    array_shift($dirListlist);
    array_shift($dirListlist);
    foreach($dirListlist as $d){
     $rd = $rdirList.'/'.$d;
     if(isset($d) && is_file($rd)){ 
      unlink($rd);
     }else{
      $this->deldir($rd);
     }
    }  
    rmdir($rdirList);
   }else{
    return false;
   }
  }
     return true;
 }  
}

用了递归方法。

//来看二个实例

$dirList="www.111cn.net/"; //指定目录
deltree($dirList);

deldir($dirList);

 

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template