Home >Backend Development >PHP Tutorial >How to read directories and files recursively in php, _PHP tutorial
The example in this article describes the method of reading directories and files recursively in PHP. Share it with everyone for your reference. The details are as follows:
This example analyzes the method of reading directories and files recursively in PHP. The code contains more detailed comments, as shown below:
<?php function showdir($path){ $dh = opendir($path);//打开目录 while(($d = readdir($dh)) != false){ //逐个文件读取,添加!=false条件,是为避免有文件或目录的名称为0 if($d=='.' || $d == '..'){//判断是否为.或..,默认都会有 continue; } echo $d."<br />"; if(is_dir($path.'/'.$d)){//如果为目录 showdir($path.'/'.$d);//继续读取该目录下的目录或文件 } } } $path = './';//当前目录 showdir($path); ?>
I hope this article will be helpful to everyone’s PHP programming design.