This example describes how PHP uses the Glob function to traverse a directory or folder. Share to everyone for your reference. The specific analysis is as follows:
When it comes to PHP traversal directory Many of our friends will think of Opendir and Readdir, so that you can traverse the directory and display files, but in PHP there is a more concise traversal of the directory function Glob estimated that very few people know this function, But I think it's much simpler than Opendir and Readdir.
Use of the PHP glob function: glob-Find the file path that matches the pattern.
example, the code is as follows:
Copy Code code as follows:
<?php
$fileList =glob (' *.* ');
for ($i =0; $i <count ($fileList); $i + +) {
echo $fileList [$i]. ' <br/> ';
}
$fileList 2=glob (' images/* ');
For ($i =0 $i <count ($fileList 2); $i + +) {
echo $fileList 2[$i]. ' <br/> ';
}
$fileList 3=glob (' * ');
For ($i =0 $i <count ($fileList 3); $i + +) {
echo $fileList 3[$i]. ' <br/> ';
}
?>
The first: The parameters of the Glob function are: *.*, which means scanning the files in the current directory, excluding folders, and returning an array, the following two cases are the same.
The second: The Glob function of the parameter inside is: images/*, is the specified directory to scan all files, including folders, can also scan the specified file types, such as: images/*.jpg; note that if you only enter: Images will only return the folder name, if you only enter: Images/will not return anything.
The third: The parameters of the Glob function are: *, you can scan the current directory of all files, directories and subdirectories of files.
Okay, let's take a look at Opendir and Readdir traversal directory, the code is as follows:
Copy Code code as follows:
<?php
/**********************
A simple directory recursive function
The first way to do this: return objects with Dir
***********************/
function tree ($directory)
{
$mydir = Dir ($directory);
echo "<ul>\n";
while ($file = $mydir->read ())
{
if ((Is_dir ("$directory/$file")) and ($file!= ".") and ($file!= "..."))
{
echo "<li><font color=\" #ff00cc \ "><b> $file </b></font></li>\n";
Tree ("$directory/$file");
}
Else
echo "<li> $file </li>\n";
}
echo "</ul>\n";
$mydir->close ();
}
Start running
echo "Tree ("./nowamagic");
/***********************
The second implementation: using the Readdir () function
************************/
function Listdir ($dir)
{
if (Is_dir ($dir))
{
if ($dh = Opendir ($dir))
{
while (($file = Readdir ($DH))!== false)
{
if (Is_dir ($dir.) /". $file)) && $file!=". && $file!= "...")
{
echo "<b><font color= ' red ' > FileName:</font></b>", $file, "<br>Listdir ($dir. " /". $file." /");
}
Else
{
if ($file!= "." && $file!= "...")
{
echo $file. " <br> ";
}
}
}
Closedir ($DH);
}
}
}
Start running
Listdir ("./nowamagic");
?>
Okay, we all see the son Glob and Opendir and readdir traversal input, from the code concise to Glob is the victory of Opendir and Readdir, in the realization of the function is the same effect, so it is recommended to use the Glob function to traverse the directory.
I hope this article will help you with your PHP program design.