PHP implementation of the directory or folder traversal, and delete the specified file code, very simple, suitable for beginners to refer to friends. The main functions are as follows: Traverse the file under this folder, Directory subdirectory, read the current file under directory and file, delete directory subdirectory and file under current folder. Note: Chinese catalogs are not supported at this stage.
Array (), ' file ' =>array ()); $HD = Opendir ($path); while (($file = Readdir ($HD))!==false) {if ($file = = "." | | $file = = "..") {continue;} if (Is_dir ($path. " /". $file)") {$arr [' dir '] [] = Iconv (' GBK ', ' utf-8 ', $file); }else if (Is_file ($path. " /". $file)") {$arr [' file '] [] = Iconv (' GBK ', ' utf-8 ', $file); }} closedir ($HD); The echo "Directory is:". Implode (" ", $arr [' dir '])." "; echo "files are:". Implode (" ", $arr [' file ']);} /*** traverse the files and directories in the current directory and the directories in subfolders * * @param string $path path * @return Array all the files that meet the criteria */function blist ($path) {i F (!is_dir (Iconv ("Utf-8", "GBK", $path)) {throw new Exception ("folder". $path. " Does not exist or is not a file "); } $arr = Array (); $HD = Opendir (Iconv ("Utf-8", "GBK", $path)); while (($file = Readdir ($HD))!==false) {if ($file = = "." | | $file = = "..") {continue;} $newpath =iconv (' utf-8 ', ' GBK ', $path). ' /'. $file; if (Is_dir ($newpath)) {$arr [] = Blist ($path. " /". $file); }else if (Is_file ($newpath)) {$arr [] = Iconv (' GBK ', ' utf-8 ', $file); }} closedir ($HD); return $arr;} /**by http://bbs.it-home.org* Delete files and subdirectories under directory * #param string $path path * #return String Delete successfully returns true failed return false;*/function Dirde L ($path) {if (!is_dir ($path)) {throw new Exception ($path. ") Input is not a valid directory "); } $hand = Opendir ($path); while (($file = Readdir ($hand))!==false) {if ($file = = "." | | $file = = "..") Continue if (Is_dir ($path. " /". $file) {Dirdel ($path. ") /". $file); }else{@unlink ($path. " /". $file); }} closedir ($hand); @rmdir ($path);}? > You may be interested in the article: PHP delete the specified file and folder method PHP Delete all files created by n minutes before the small example of PHP instance: bulk Delete files in folders and folders php delete directory and all Files Method Example PHP directory traversal and delete function sample PHP RmDir Delete directory Three ways to delete all files in the specified folder PHP code delete multi-level directory PHP custom function php Delete directory and list directory All files code PHP recursive delete files and directories code PHP recursive delete directory and multi-level sub-directory All Files code PHP recursively create and delete folder code Example of PHP recursive delete directory |