php遞迴刪除目錄下的所有檔案:
<?php header("content-type:text/html;charset=utf-8"); /** *刪除指定目錄()刪除子目錄和檔案 *@path 檔案目錄路徑 string *@return void */function hello($path){ //1:判斷刪除目錄是否存在 if(!file_exists($path)){ return false; } //2:將目錄內容全部擷取出 $list = scandir($path); //3:遍曆目錄 foreach($list as $f){ //4:將 . .. 排除在外 if($f != '.' && $f != '..'){ //5:如果內容檔案 unlink if(is_file($path."/".$f)){ unlink($path.".".$f); }else{ //6:目錄 遞迴 hello($path."/".$f); } } }//foreach end //7:迴圈外刪除目錄!! rmdir($path); } ?>
php遞迴便利出目錄下的所有檔案:
<?php header('content-type:text/html;charset=gbk'); ini_set("date.timezone", "Asia/Chongqing"); /* * 遍曆一個指定目錄()包括子目錄和檔案 * @param string $path 指定目錄名稱 * @return viod */class dir{ function upl($path){ //判斷處理的目錄是否存在 不存在 return false; if(!file_exists($path)){ return false; } //列出目前的目錄內容 $list=scandir($path); foreach($list as $f){ //去除 . .. if($f!='.'&&$f!='..'){ //判斷是否是一個目錄【$path.'/'.$f】 if(is_dir($path."/".$f)){ //輸出 echo $path."/".$f."<br />"; //遞迴調用自己 $this->upl($path."/".$f); }else{ //如果檔案存在輸出 echo $path."/".$f."<br />"; } }//if end }//foreach end } } $a=new dir(); $a->upl("d:/www/guo/application");