PHP recursive and non-recursive traversal of all files in the directory

Source: Internet
Author: User
PHP recursive implementation traversal with Dir return object
 Read ()) {//directory with hidden file '. ') and '. ' When traversing requires attention to if ((Is_dir ("$dir/$file") && ($file! = ".") && ($file! = "...")) {echo $file. '
'; Loop ("$dir/$file"); Recursive loop}else{if ($file! = ":" && $file! = ".") {echo $file. "
"; }}}} loop (DirName (__file__)); DirName Remove file name return directory name//non-recursive processing traverse directory//idea: First create an array, because the first pass is a directory name minus the file name (such as c://wamp/www/php)//For the Foreach Loop so the first time to put c://wamp/ All the files under www/php are put into the array//only enough while cycle the last one of each output array, when the file is a directory, the Foreach Loop//Know the last value when the Count ($list) value is 0 exit the loop function ScanAll ($dir) {$list = array (); $list [] = $dir; while (count ($list) > 0) {//Var_dump ($list); The last element of the popup array $file = Array_pop ($list); Handles the current file echo $file.
"; If it is a directory if (Is_dir ($file)) {$children = Scandir ($file); Var_dump ($children); foreach ($children as $child) {if ($child!== '. ' && $child!== ') {$list [] = $file. ' /'. $child; }}}}}scanall (DirName (__file__));
  • 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.