5 Methods of implementing recursive directory in PHP _php instance

Source: Internet
Author: User

In the development of the project, it is unavoidable to create folders on the server, such as the directory when uploading pictures, the directory of template parsing. This does not apply to the current project, so it summarizes several ways to create a directory.

Method One: Use Glob loop

<?php
//Method One: Use Glob cyclic
 
function Myscandir1 ($path, & $arr) {
 
  foreach (Glob ($path) as $file) {
    if ( Is_dir ($file)) {
      Myscandir1 ($file. '/* ', $arr);
    } else {
 
      $arr [] = Realpath ($file);
    }
  }
? >

Method Two: Use the dir && read loop

<?php
//Method Two: Use dir && read loop
function Myscandir2 ($path, & $arr) {
 
  $dir _handle = Dir ($path );
  while ($file = $dir _handle->read ())!== false) {
 
    $p = Realpath ($path. '/' . $file);
    if ($file!= "." && $file!= "...") {
      $arr [] = $p;
    }
 
    if (Is_dir ($p) && $file!= "." && $file!= "...") {
      Myscandir2 ($p, $arr);
    }
  }
? > 

Method Three: Use Opendir && readdir cycle

<?php
//Method III: Use Opendir && readdir cyclic
function Myscandir3 ($path, & $arr) {
   
  $dir _handle = Opendir ($path);
  while ($file = Readdir ($dir _handle))!== false) {
 
    $p = Realpath ($path. '/' . $file);
    if ($file!= "." && $file!= "...") {
      $arr [] = $p;
    }
    if (Is_dir ($p) && $file!= "." && $file!= "...") {
      myscandir3 ($p, $arr);
    }
  }
 ? >

Method Four: Use Scandir loop

<?php
//Method IV: Use SCANDIR cyclic
function Myscandir4 ($path, & $arr) {
   
  $dir _handle = Scandir ($path);
  foreach ($dir _handle as $file) {
 
    $p = Realpath ($path. '/' . $file);
    if ($file!= "." && $file!= "...") {
      $arr [] = $p;
    }
    if (Is_dir ($p) && $file!= "." && $file!= "...") {
      myscandir4 ($p, $arr);
    }
  }
 ? >

Method V: Using SPL loops

 <?php
//Method Five: Use SPL cyclic
function Myscandir5 ($path, & $arr) {
 
  $iterator = new Directoryiterator ($ path);
  foreach ($iterator as $fileinfo) {
 
    $file = $fileinfo->getfilename ();
    $p = Realpath ($path. '/' . $file);
    if (! $fileinfo->isdot ()) {
      $arr [] = $p;
    }
    if ($fileinfo->isdir () &&! $fileinfo->isdot ()) {
      myscandir5 ($p, $arr);
    }
  }
? > 

You can use the Xdebug test run time

<?php
Myscandir1 ('./code ', $arr 1);//0.164010047913 
Myscandir2 ('./code ', $arr 2);//0.243014097214 
Myscandir3 ('./code ', $arr 3);//0.233012914658 
myscandir4 ('./code ', $arr 4);//0.240014076233
myscandir5 ('./ Code ', $arr 5);//0.329999923706
 
 
//need to install Xdebug
echo xdebug_time_index (), "\ n";
? >

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.