php遞迴遍曆和和刪除檔案夾下的所有檔案

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.