php遍历目录的方法

<p>本文实例总结了PHP遍历目录文件的常用方法。分享给大家供大家参考,具体如下:<br/></p><p>测试算法(源代码经过本站工具http://tools.jb51.net/code/jb51_php_format进行格式化处理,以便于读者阅读)</p><p>算法1.简短系</p><pre class="brush:php;toolbar:false">foreach(glob(&#39;*.*&#39;)as$filename) { echo&#39;Filename:&#39;.$filename.; }</pre><p>算法2.规矩系</p><pre class="brush:php;toolbar:false">if($handle=opendir(&#39;C:\\Inetpub\\wwwroot\\test\\&#39;)){ echo&quot;Files:\n&quot;; while(false!==($file=readdir($handle))){ echo&quot;$file\n&quot;; } closedir($handle); }</pre><p>算法3.函数系</p><pre class="brush:php;toolbar:false">functiontree($directory) { $mydir=dir($directory); while($file=$mydir-&gt;read()){ if((is_dir(&quot;$directory/$file&quot;))AND($file!=&quot;.&quot;)AND($file!=&quot;..&quot;)) { echo&quot;$file\n&quot;; tree(&quot;$directory/$file&quot;); }else echo&quot;$file\n&quot;; } echo&quot;\n&quot;; $mydir-&gt;close(); } tree(&quot;C:\\Inetpub\\wwwroot\\test\\&quot;);</pre><p>算法4.函数系II</p><pre class="brush:php;toolbar:false">functionlistDir($dir){ if(is_dir($dir)){ if($dh=opendir($dir)){ while(($file=readdir($dh))!==false){ if((is_dir($dir.&quot;/&quot;.$file))&amp;&amp;$file!=&quot;.&quot;&amp;&amp;$file!=&quot;..&quot;){ echo&quot;文件名:&quot;,$file; listDir($dir.&quot;/&quot;.$file.&quot;/&quot;); }else{ if($file!=&quot;.&quot;&amp;&amp;$file!=&quot;..&quot;){ echo$file; } } } closedir($dh); } } } listDir(&quot;C:\\Inetpub\\wwwroot\\test\\&quot;);</pre><p>算法5.递归系</p><pre class="brush:php;toolbar:false">functionfile_list($dir,$pattern=&quot;&quot;) { $arr=array(); $dir_handle=opendir($dir); if($dir_handle) { while(($file=readdir($dir_handle))!==false) { if($file===&#39;.&#39;||$file===&#39;..&#39;) { continue; } $tmp=realpath($dir.&#39;/&#39;.$file); if(is_dir($tmp)) { $retArr=file_list($tmp,$pattern); if(!emptyempty($retArr)) { $arr[]=$retArr; } }else { if($pattern===&quot;&quot;||preg_match($pattern,$tmp)) { $arr[]=$tmp; } } } closedir($dir_handle); } return$arr; } print_r(file_list(&quot;C:\\Inetpub\\wwwroot\\test\\&quot;));</pre><p>测试方法</p><p>我们采取在测试代码的头部和尾部添加如下的内容来检测执行时间,并测试5次取平均结果作为最终成绩。</p><pre class="brush:php;toolbar:false"> $stime=microtime(true); //测试代码 //...... //...... $etime=microtime(true); $total=($etime-$stime)*1000; echo&quot;{$total}Millisecond(s)&quot;;</pre><p>测试结果:</p><p>算法1</p><p>算法1在浏览器能正确输出所有项目,5次测验耗费的时间分别是:</p><p>平均用时=3803.618621824 毫秒</p><p>算法2</p><p>算法2在浏览器也能正确输出所有项目,但在开头会给出&quot;..&quot;(上级目录)的信息。5次测验耗费的时间分别是:</p><p>平均用时=381.0853481294 毫秒</p><p>算法3</p><p>算法3在浏览器能正确输出所有项目,也仍会给出&quot;..&quot;(上级目录)的信息。5次测验耗费的时间分别是:</p><p>平均用时=24299.2805485 毫秒</p><p>算法4</p><p>算法4和算法3类似,在浏览器能正确输出所有项目,5次测验耗费的时间分别是:</p><p>平均用时=24020.66812516 毫秒</p><p>算法5</p><p>算法5曾一度让我以为IIS又出问题了。虽说它在浏览器能正确输出所有项目,但数据的结果默认为数组。5次测验耗费的时间分别是:</p><p>平均用时=61411.31243706 毫秒</p><p>测试总结</p><p>根据测试结果,我们很容易得出下面的速度排名。</p><p>算法2 &gt; 算法1 &gt; 算法4 &gt; 算法3 &gt; 算法5</p><p>为什么算法2要比其他算法都高效一些呢?</p><p>实际上是因为算法中只使用了php中内置用来读取目录内容的函数&quot;readdir()&quot; 。除了算法1以外,其他算法在引用readdir()的时候,为了弥补函数的先天不足,干了很多其他的事情。</p><p>如果说,我们需要指定扩展名的列举目录内所有文件的话。Rt推荐使用算法1的模式,我们将代码写成这样就可以了。</p><pre class="brush:php;toolbar:false">foreach(glob(&#39;*.需要的扩展名&#39;)as$filename) { echo&#39;Filename:&#39;.$filename.; }</pre><p>结语</p><p>不管作为一个代码新手还是一个资深的程序员,在堆砌代码的时候,更应该关注程序的效率和安全。</p><p>请不要忽略任何一种可能,也请不要让孩子般的代码成为拖累服务器的罪魁祸首。</p>
RangeTime:0.003740s
RangeMem:203.26 KB
返回顶部 留言