PHP recursively traverses and deletes all files under the folder

Source: Internet
Author: User

PHP recursively deletes all files in the directory:

<?php  
Header ("Content-type:text/html;charset=utf-8");  
/** 
* Delete specified directory () Delete subdirectories and files 
* @path  file directory path string 
* @return void
/function Hello ($path) {  
// 1: Determine if the deletion directory exists if  
(!file_exists ($path)) {return  
  false;  
}  
2: The contents of the catalog are all obtained  
$list = Scandir ($path);  
3: Traverse directory  
foreach ($list as $f) {  
//4: Will ...  exclude outside  
if ($f!= '. ' && $f!= ') {  
 //5: If the content file unlink if  
 (Is_file ($path.) /". $f)) {  
  unlink ($path.".). $f);  
 } else{  
   //6: Table of Contents   recursive  
   Hello ($path.) /". $f);}}  
foreach End  
//7: Delete directory outside of loop!!  
RmDir ($path);  
      
>

PHP recursion facilitates all files in the directory:

<?php  
Header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');  
Ini_set ("Date.timezone", "asia/chongqing"); 
 * * Traverse a specified directory () including subdirectories and file 
 * @param string $path Specify directory name 
 * @return viod
/class dir{  
function UPL ( $path) {  
//To determine whether the directory being processed has   no return false;  
if (!file_exists ($path)) {return  
false;  
}  
Lists the contents of the current directory  
$list =scandir ($path);  
foreach ($list as $f) {  
//Remove ...  
if ($f!= '. ') && $f!= ' ... ') {  
//judge whether it is a directory "$path." /'. $f '  
if (Is_dir ($path.) /". $f)) {//  
output      
echo $path." /". $f." <br  /> ";  
Recursively calls itself  
$this->upl ($path. " /". $f);  
} else{  
//If file exists output  
echo $path. " /". $f." <br  /> ";  
}  
} If End  
      
}//foreach end  
}  
}  
$a =new dir ();  
$a->upl ("d:/www/guo/application");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.