//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