The example in this article describes how PHP uses the glob function to traverse directories or folders. Share it with everyone for your reference. The specific analysis is as follows:
When it comes to traversing directories in PHP, many of our friends will think of opendir and readdir, which can traverse directories and display files. However, there is a more concise function glob in PHP that traverses directories. It is estimated that few people know about it. This function, but I think it is much simpler than opendir and readdir.
Usage of PHP glob function: glob—find the file path that matches the pattern.
Example, the code is as follows:
<?php $fileList=glob('*.*'); for ($i=0; $i<count($fileList); $i++) { echo $fileList[$i].'<br />'; } $fileList2=glob('images/*'); for ($i=0; $i<count($fileList2); $i++) { echo $fileList2[$i].'<br />'; } $fileList3=glob('*'); for ($i=0; $i<count($fileList3); $i++) { echo $fileList3[$i].'<br />'; } ?>
First type: glob function The parameters are: *.*, which means scanning files in the current directory, excluding folders, and returning an array. The following two situations are the same.
The second one: the parameters of the glob function are :images/* specifies a directory to scan all files, including folders, and can also scan specified file types, such as: images/*.jpg; note that if you only enter: images, only the folder name will be returned. If you enter: images/, nothing will be returned.
The third type: the parameters of the glob function are: *, which can scan out all files, directories and subdirectory files in the current directory.
Okay, let’s take a look opendir and readdir traverse the directory, the code is as follows:
<?php /********************** 一个简单的目录递归函数 第一种实现办法:用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(); } //开始运行 echo "<h2>目录为粉红色</h2><br>\n"; tree("./nowamagic"); /*********************** 第二种实现办法:用readdir()函数 ************************/ 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'>文件名:</font></b>",$file,"<br><hr>"; listDir($dir."/".$file."/"); } else { if($file!="." && $file!="..") { echo $file."<br>"; } } } closedir($dh); } } } //开始运行 listDir("./nowamagic"); ?>
Okay, everyone has seen how to write sub-glob, opendir and readdir to traverse the input. In terms of code simplicity, glob completely beats opendir and readdir, and what it achieves in terms of functional implementation is The same effect, so it is recommended to use the glob function to traverse the directory.
I hope this article will be helpful to everyone’s PHP programming design.
For more related articles on how PHP uses the glob function to traverse directories or folders, please pay attention to the PHP Chinese website!