Home > php教程 > PHP开发 > body text

How to traverse directories or folders using glob function in PHP

高洛峰
Release: 2016-12-21 16:20:42
Original
1094 people have browsed it

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(&#39;*.*&#39;); 
for ($i=0; $i<count($fileList); $i++) { 
echo $fileList[$i].&#39;<br />&#39;; 
} 
$fileList2=glob(&#39;images/*&#39;); 
for ($i=0; $i<count($fileList2); $i++) { 
echo $fileList2[$i].&#39;<br />&#39;; 
} 
$fileList3=glob(&#39;*&#39;); 
for ($i=0; $i<count($fileList3); $i++) { 
echo $fileList3[$i].&#39;<br />&#39;; 
} 
?>
Copy after login

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=&#39;red&#39;>文件名:</font></b>",$file,"<br><hr>"; 
listDir($dir."/".$file."/");
} 
else 
{ 
if($file!="." && $file!="..") 
{ 
echo $file."<br>"; 
 } 
} 
   } 
   closedir($dh); 
} 
   } 
} 
//开始运行 
listDir("./nowamagic"); 
?>
Copy after login

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!


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!