A sample code for PHP directory traversal and deletion

Source: Internet
Author: User
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
  • Related Article

    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.