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方法:刪除目錄檔案
?