Home  >  Article  >  Backend Development  >  thinkphp清空全部目录(包括子目录)

thinkphp清空全部目录(包括子目录)

WBOY
WBOYOriginal
2016-06-13 13:08:29775browse

thinkphp清空所有目录(包括子目录)

在admin项目的Common目录下common.php文件:

/**
*   删除xml目录下的所有xml文件
*   string $fp  文件路径(不包括文件名)
*   string $fn  文件名称(包括扩展名)
*   boolean $type 是否关联到所有子目录
*/
function delXML($type=true,$fn='',$fp='./xml'){
 if(!is_dir($fp)){
  return 'nodir';  //被删除目录不存在
 }else{
  if(!is_empty_dir($fp)){//如果不是空的     
   $H = @ opendir($fp);
   while(false !== ($_file=readdir($H))){
    //检索目录
    if(is_dir($fp."/".$_file) && $_file != "." && $_file!=".." && $_file!=="Thumbs.db"){
     if($type){
      if(!is_empty_dir($fp.'/'.$_file)){//如果不是,调用自身,不过是原来的路径+他下级的目录名   
       delXML($type,$fn,$fp."/".$_file);
      }   
      if(is_empty_dir($fp.'/'.$_file)){//如果是空就直接删除   
       rmdir($fp.'/'.$_file);   
      }   
     }
    //检索文件
    }else if(is_file($fp."/".$_file) && $_file!="." && $_file!=".." && $_file!=="Thumbs.db"){
     if(eregi('/'.$file,'/'.$_file)){
      if(!unlink($fp.'/'.$_file)){
       return false; //删除失败
      }
     }
    }
   }
   closedir($H);  
  }   
  return true;   //删除失败
 }
}   

//判断目录是否为空,true为空,false为不空
function is_empty_dir($fp)   
{   
 $H = @ opendir($fp);
 $i=0;   
 while($_file=readdir($H)){   
  $i++;   
 }   
 closedir($H);   
 if($i>2){
  return false;
 }else{
  return true;  
 }
}

 

调用方法:

在admin项目的Action目录下SiteMapAction.class.php文件:

class SiteMapAction extends Action {

 //删除全部xml目录地图文件
 function delXML(){
  $flag = delXML();
  exit($flag);
 }

}

 

静态页面ajax方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../Public/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/js/artDialog/artDialog.min.js"></script>
<title>删除目录文件</title>
</head>

<body>
<script language="javascript">

function delXML(){
 if(!window.confirm('确定要删除整站地图吗?')){
  return; 
 }
 var testDialog;
 testDialog = art.dialog({
  lock: false,
  title:'',
  id:'loaddialog',
  content:'<font color="#ff0000">正在删除中...</font>',
  width:220
 });
 var url="{:U('delXML')}";
 $.get(url,null,function(data){
  //$("#con").append(data);
  testDialog.close();
  if(data == true){
   alert('删除成功!');
  }else if(data == false){
   alert('删除失败,请稍候再试...');
  }else if(data == 'nodir'){
   alert('错误:被删除目录不存在'); 
  }
 });
}
</script>
<input name="delete" type="button" class="agin agn"  value="全删除" onClick="javascript:return delXML();"/>
</body>
</html>

?

Statement:
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