Example of all files and subfolders under the PHP Traversal folder

Source: Internet
Author: User

  This article mainly introduces the PHP traversal folder of all the files and subfolders examples, here are two methods, the need for friends can refer to the following

Traverse the directory and the result is stored in an array. Support PHP4 and above. PHP5 can replace the while loop with the Scandir () function later.   Code as follows: <?php/** * @param string $dir * @return Array/function My_scandir ($dir) {  $files = array (); &nbs P;if ($handle = Opendir ($dir)) {  while ($file = Readdir ($handle))!== false)     {   IF ( $file!= "..." && $file!= ".")      {    if Is_dir ($dir. "/" . $file))       {      $files [$file] = My_scandir ($dir. "/" . $file);    }     else      {      $files [] = $file;    }   &nbs P;}  }   Closedir ($handle);   return $files; &NBSP}   function My_scandir1 ($dir) {  $files = array ();   $dir _list = Scandir ($dir);  foreach ($dir _ List as $file)  {  if ($file!= "..." && $file!= ".")     {   if is_dir ($dir. "/" . $file))      {    $files [$file] = My_scandir1($dir. "/" . $file);   &nbsp    else     {    $files [] = $file;    }  }   &nbs P  return $files; }   $result = My_scandir ('./'); $result = My_scandir1 ('./');?>       Another implementation method     code is as follows: function Fetchdir ($dir) {          foreach (Glob $dir. ' * ') as $file) {              echo $file, "n"; &n Bsp             if (Is_dir ($file)) {                &NBSP ; Fetchdir ($file);             }         }      }      Fetchdir ("D:wampwwwany");      can also replace ' * ' with directory_separator. ' * ', replace ' n ' with Php_eol, so you can cross the platform.  

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.