Home > Backend Development > PHP Tutorial > 5 ways to implement recursive directories in PHP

5 ways to implement recursive directories in PHP

墨辰丷
Release: 2023-03-28 15:42:01
Original
1833 people have browsed it

This article mainly introduces 5 ways to implement recursive directories in PHP, which are mainly implemented by using some loops. Interested friends can refer to it.

During project development, it is inevitable to create folders on the server, such as the directory when uploading images, the directory when parsing templates, etc. This is not used in my current project, so I summarized several methods of creating directories in a loop.

Method 1: Use glob loop

<?php
//方法一:使用glob循环
 
function myscandir1($path, &$arr) {
 
  foreach (glob($path) as $file) {
    if (is_dir($file)) {
      myscandir1($file . &#39;/*&#39;, $arr);
    } else {
 
      $arr[] = realpath($file);
    }
  }
}
?>
Copy after login

Method 2: Use dir && read loop

<?php
//方法二:使用dir && read循环
function myscandir2($path, &$arr) {
 
  $dir_handle = dir($path);
  while (($file = $dir_handle->read()) !== false) {
 
    $p = realpath($path . &#39;/&#39; . $file);
    if ($file != "." && $file != "..") {
      $arr[] = $p;
    }
 
    if (is_dir($p) && $file != "." && $file != "..") {
      myscandir2($p, $arr);
    }
  }
}
?>
Copy after login

Method 3: Use opendir && readdir loop

<?php
//方法三:使用opendir && readdir循环
function myscandir3($path, &$arr) {
   
  $dir_handle = opendir($path);
  while (($file = readdir($dir_handle)) !== false) {
 
    $p = realpath($path . &#39;/&#39; . $file);
    if ($file != "." && $file != "..") {
      $arr[] = $p;
    }
    if (is_dir($p) && $file != "." && $file != "..") {
      myscandir3($p, $arr);
    }
  }
}
 ?>
Copy after login

Method 4: Use scandir loop

<?php
//方法四:使用scandir循环
function myscandir4($path, &$arr) {
   
  $dir_handle = scandir($path);
  foreach ($dir_handle as $file) {
 
    $p = realpath($path . &#39;/&#39; . $file);
    if ($file != "." && $file != "..") {
      $arr[] = $p;
    }
    if (is_dir($p) && $file != "." && $file != "..") {
      myscandir4($p, $arr);
    }
  }
}
 ?>
Copy after login

Method 5: Use SPL loop

##

 <?php
//方法五:使用SPL循环
function myscandir5($path, &$arr) {
 
  $iterator = new DirectoryIterator($path);
  foreach ($iterator as $fileinfo) {
 
    $file = $fileinfo->getFilename();
    $p = realpath($path . &#39;/&#39; . $file);
    if (!$fileinfo->isDot()) {
      $arr[] = $p;
    }
    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
      myscandir5($p, $arr);
    }
  }
}
?>
Copy after login

You can use xdebug to test the running time


<?php
myscandir1(&#39;./Code&#39;,$arr1);//0.164010047913 
myscandir2(&#39;./Code&#39;,$arr2);//0.243014097214 
myscandir3(&#39;./Code&#39;,$arr3);//0.233012914658 
myscandir4(&#39;./Code&#39;,$arr4);//0.240014076233
myscandir5(&#39;./Code&#39;,$arr5);//0.329999923706
 
 
//需要安装xdebug
echo xdebug_time_index(), "\n";
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

Redis multi-database selection function singleton class implemented in PHP (detailed explanation)

Detailed explanation of the method of PHP custom function to determine whether it is submitted by Get/Post/Ajax

phpMethod of automatically backing up the database table

The above is the detailed content of 5 ways to implement recursive directories in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template