大家现在也许对但是递归函数是一个很复杂而且杂乱的东西,大多数开发者(包括我自己) 并不是真正喜欢使用它们。这就是为什么当我的上一个项目需要为一个单独文件扫描一个目录层级时(一个典型的递归函数任务),我甚至没有考虑过要循环自己的 代码。我取而代之地直接使用了PEAR和它的File_Find类,免除了我搜索多层目录结构的痛苦。
File_Find类是为了能让你真 正做好两件事情而设计的。第一,它可以让你扫描一个目录树,然后将其转换成为PHP结构——一个嵌套的数组——它反映了原始层级的父子关系。第二,它可以 让你在一个目录树下搜索一个或多个类型相匹配的文件。这种类型可以是一个简单的字符串,也可以是一个复杂的Perl规则的表达式;File_Find能够 处理上述两种中的任意一种格式,并将返回一个包括了每一对的详细文件和路径信息的数组。
一开始,手动安装程序包,你可以下载它,然后将其内容提取到PEAR根目录下,也可以利用PEAR安装程序。
接下来,创建以下PHP脚本(列表A),并将它保存在你的Web服务器的文件根目录下:
PHP类搜索定位目录树列表A
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>// include class </span></li> <li class="alt"><span>include(”Find.php”); </span></li> <li><span>// initialize finder </span></li> <li class="alt"> <span>$</span><span class="attribute">finder</span><span> = </span><span class="attribute-value">new</span><span> File_Find(); </span> </li> <li><span>// read directory tree and print </span></li> <li class="alt"> <span>$</span><span class="attribute">tree</span><span> = $finder-</span><span class="tag">></span><span>mapTree(”/tmp”); </span> </li> <li><span>print_r($tree); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
在此,我初始化了一个新的File_Find()对象, 并用一个目录路径调用其mapTree()方法。mapTree()方法读取指定的目录,然后产生它的一个两元素的层级结构“数组表(array map)”。此数组中第一个元素列出了所找到的所有目标目录下的子目录,而第二个元素列出了找到的所有文件。然后就有可能扫描这些序列,在应用程序中使用 它们了——例如,重建目录树,或者在自定义标准的基础上筛选文件和目录。
列表B是以上脚本输出的一个例子:
PHP类搜索定位目录树列表B
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> Array </span> </li> <li><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> /tmp </span> </li> <li> <span>[1] =</span><span class="tag">></span><span> /tmp/dummyA </span> </li> <li class="alt"> <span>[2] =</span><span class="tag">></span><span> /tmp/dummyB </span> </li> <li> <span>[3] =</span><span class="tag">></span><span> /tmp/dummyA/dummyC </span> </li> <li class="alt"><span>) </span></li> <li> <span>[1] =</span><span class="tag">></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li> <span>[0] =</span><span class="tag">></span><span> /tmp/data.txt </span> </li> <li class="alt"> <span>[1] =</span><span class="tag">></span><span> /tmp/dummyB/metoo.mp3 </span> </li> <li> <span>[2] =</span><span class="tag">></span><span> /tmp/dummyB/track.dat </span> </li> <li class="alt"> <span>[3] =</span><span class="tag">></span><span> /tmp/dummyA/dummyC/parrot.gif </span> </li> <li><span>) </span></li> <li class="alt"><span>) </span></li> </ol>
你也可以用另一种方式,用mapTreeMultiple()方法,它递归读取指定的目录,产生一个复制其树形结构的嵌套数组。列表C为你展示了一个例子。
PHP类搜索定位目录树列表C
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>// include class </span></li> <li class="alt"><span>include(”Find.php”); </span></li> <li><span>// initialize finder </span></li> <li class="alt"> <span>$</span><span class="attribute">finder</span><span> = </span><span class="attribute-value">new</span><span> File_Find(); </span> </li> <li><span>// print recursive directory tree </span></li> <li class="alt"> <span>$</span><span class="attribute">tree</span><span> = $finder-</span><span class="tag">></span><span>mapTreeMultiple(”/tmp”); </span> </li> <li><span>print_r($tree); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
列表D显示了输出结果:
PHP类搜索定位目录树列表D
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> data.txt </span> </li> <li> <span>[dummyA] =</span><span class="tag">></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li> <span>[dummyC] =</span><span class="tag">></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li> <span>[0] =</span><span class="tag">></span><span> parrot.gif </span> </li> <li class="alt"><span>) </span></li> <li><span>) </span></li> <li class="alt"> <span>[dummyB] =</span><span class="tag">></span><span> Array </span> </li> <li><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> metoo.mp3 </span> </li> <li> <span>[1] =</span><span class="tag">></span><span> track.dat </span> </li> <li class="alt"><span>) </span></li> <li><span>) </span></li> </ol>
提示:你可以给mapTreeMultiple()加上一个可选的参数,限定它在执行递归时向下查询的层数。
用glob()方法,你可以在指定目录下搜索与特定Perl兼容型的规则表达式匹配的文件。请看一下列表E。
PHP类搜索定位目录树列表E
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>// include class </span></li> <li class="alt"><span>include(”Find.php”); </span></li> <li><span>// initialize finder </span></li> <li class="alt"> <span>$</span><span class="attribute">finder</span><span> = </span><span class="attribute-value">new</span><span> File_Find(); </span> </li> <li><span>// search for matching files in named directory </span></li> <li class="alt"> <span>$</span><span class="attribute">results</span><span> = $finder-</span><span class="tag">></span><span>glob(”/mp3/i”, “/usr/local/stuff”, “perl”); </span> </li> <li><span>print_r($results); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
在这里,glob()方法访问指定目录下所有名称中带有字符串mp3的文件。注意我对修饰符i的使用,它使得搜索没有大小写之分,还有我使用了第三个自变量,告诉类要使用Perl regex函数,而不是默认的PHP函数。
File_Find类由于它的search()方法而真正发挥作用起来,这个方法结合了mapTreeMultiple()和glob()方法的能力进行多层目录搜索。列表F中是一个例子。
PHP类搜索定位目录树列表F
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>// include class </span></li> <li class="alt"><span>include(”Find.php”); </span></li> <li><span>// initialize finder </span></li> <li class="alt"> <span>$</span><span class="attribute">finder</span><span> = </span><span class="attribute-value">new</span><span> File_Find(); </span> </li> <li><span>// search for matching files </span></li> <li class="alt"><span>// in named directory and subdirectories </span></li> <li> <span>$</span><span class="attribute">results</span><span> = $finder-</span><span class="tag">></span><span>search(”/exe/i”, “/usr/local/winstuff”, “perl”); </span> </li> <li class="alt"><span>print_r($results); </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
列表G显示了输出的一个样本:
PHP类搜索定位目录树列表G
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> /usr/local/winstuff/4HELP.EXE </span> </li> <li> <span>[1] =</span><span class="tag">></span><span> /usr/local/winstuff/ARJ.EXE </span> </li> <li class="alt"> <span>[2] =</span><span class="tag">></span><span> /usr/local/winstuff/bzip2.exe </span> </li> <li> <span>[3] =</span><span class="tag">></span><span> /usr/local/winstuff/CRLF.EXE </span> </li> <li class="alt"> <span>[4] =</span><span class="tag">></span><span> /usr/local/winstuff/DECODE.EXE </span> </li> <li> <span>[5] =</span><span class="tag">></span><span> /usr/local/winstuff/GREP.EXE </span> </li> <li class="alt"> <span>[6] =</span><span class="tag">></span><span> /usr/local/winstuff/GPG/gpg.exe </span> </li> <li> <span>[7] =</span><span class="tag">></span><span> /usr/local/winstuff/GPG/uninst-gnupg.exe </span> </li> <li class="alt"> <span>[8] =</span><span class="tag">></span><span> /usr/local/winstuff/GPG/WinPT.exe </span> </li> <li> <span>[9] =</span><span class="tag">></span><span> /usr/local/winstuff/Fprot/F-PROT.EXE </span> </li> <li class="alt"><span>) </span></li> </ol>
这就是你的递归目录查询,仅仅只有四行代码!
正如你所看到的那样,File_Find使得简单有效地完成一个相对复杂的任务成为可能,而且你会感到最小的压力,这也就是我为什么极力向你推荐它的原因。下次当你需要在PHP类搜索定位目录树上执行一个文件查找操作时,不妨你自己尝试一下。祝你编程快乐!