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.