search
Homephp教程PHP开发How to traverse directories or folders using glob function in PHP

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;; 
} 
?>

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 id="目录为粉红色">目录为粉红色</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"); 
?>

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!


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment