Heim  >  Artikel  >  Backend-Entwicklung  >  php递归法读取目录及文件的方法_php技巧

php递归法读取目录及文件的方法_php技巧

WBOY
WBOYOriginal
2016-05-16 20:24:32721Durchsuche

本文实例讲述了php递归法读取目录及文件的方法。分享给大家供大家参考。具体如下:

这里实例分析php递归法读取目录及文件的方法,代码中包含较为详尽的注释,如下所示:

<&#63;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);
&#63;>

希望本文所述对大家的php程序设计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn