PHP Delete directory delete files keep directory

Source: Internet
Author: User

   //Loop Delete directory and file function delete all files in directory and directory     Public Static functionDeldirandfile ($dirName )  {        //Var_dump (Is_dir ($dirName)); exit;        if($handle=Opendir( "$dirName" ) ) {               while(false!== ($item=Readdir($handle ) ) ) {                  if($item! = "." &&$item!= ".." ) {                      if(Is_dir( "$dirName/$item") ) { self::d Eldirandfile ("$dirName/$item" );//is the path continues to recursion}Else {                          unlink( "$dirName/$item" );//not path Delete file directory/directory under filename with suffix//if (unlink ("$dirName/$item")) echo "successfully deleted file: $dirName/$item <br/&  Gt;\n ";                     }                  }              }             Closedir($handle ); rmdir($dirName ); //if (rmdir ($dirName)) echo "Successfully deleted directory: $dirName <br/>\n";         }      }      //all files in the Recycle directory only delete files under the specified directory, do not delete the directory folder     Public Static functionDelfileunderdir ($dirName,$now)  {          if($handle=Opendir( "$dirName" ) ) {              while(false!== ($item=Readdir($handle ) ) ) {                  if($item! = "." &&$item!= ".." ) {                      if(Is_dir( "$dirName/$item") ) { self::d Elfileunderdir ("$dirName/$item" ); } Else {                          Preg_match("/\d+/",$item,$matches); $time=$matches[0]; $difference=Strtotime($now)-Strtotime($time); $days=intval($difference/86400); $remain=$difference%86400; $hours=intval($remain/3600); if($hours>1 | |$days>1 ){                           unlink( "$dirName/$item"); }                        //if (unlink ("$dirName/$item")) echo "Successfully deleted files: $dirName/$item <br/>\n";                     }                  }             }             Closedir($handle ); }      }  

PHP Delete directory delete files keep directory

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.