一、序言
在写上一篇博客的时候,需要用到目录树结构来展示我的文件结构,于是乎吃力不讨好的把所有文件夹和文件都手动的”遍历“了一边。后来我就想,这样太容易出错了,而且很费力气,于是就想着写个php脚本来遍历一个目录下面的文件及文件夹,并生成目录树,方便以后再有需要用到目录树结构的地方,直接运行就行了。目前脚本生成的目录树结构可以直接通过浏览器查看,也可以下载生成txt文件。
二、思路介绍
生成目录树的思路很简单,遍历当前文件夹下面的内容,碰到”.“和”..“直接跳过,碰到文件夹则递归调用,碰到文件则先保存到数组中,等当前文件夹遍历完之后吧数组中的文件拼接起来,这个操作就是生成目录树了,生成之后还有一步操作,显示或者下载目录树。书写的过程中还有一些细节问题,这个要等到开发的时候才会有。为了好理解,好扩展,我将本来一个函数可以完成的事放入一个类中,让遍历文件夹的思路更加清晰。
三、代码实现
思路有了,写起代码来就如鱼得水(这也是一般为什么厉害的人告诉我们他写代码的时候甚至有超过一般的时间在思考,而不是立马动手写代码),好了看看部分代码:
3.1 生成目录树
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * 生成目录树 </span><span> 3</span> <span>*/</span> <span> 4</span> <span>public</span> <span>function</span> createTree(<span>$path</span>, <span>$level</span>=0<span>){ </span><span> 5</span> <span>$level</span> = <span>$level</span><span>; </span><span> 6</span> <span>$this</span>->tree .= <span>str_repeat</span>(<span>$this</span>->options["padding"], <span>$level</span><span>) </span><span> 7</span> .<span>$this</span>->options["dirpre"<span>] </span><span> 8</span> .<span>$this</span>->_basename(<span>$path</span><span>) </span><span> 9</span> .<span>$this</span>->options["newline"<span>]; </span><span>10</span> <span>$level</span>++<span>; </span><span>11</span> <span>$dirHandle</span> = <span>opendir</span>(<span>$path</span><span>); </span><span>12</span> <span>$files</span> = <span>array</span><span>(); </span><span>13</span> <span>while</span> (<span>false</span> !== (<span>$dir</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) { </span><span>14</span> <span>if</span>(<span>$dir</span> == "." || <span>$dir</span> == ".."<span>){ </span><span>15</span> <span>continue</span><span>; </span><span>16</span> <span> } </span><span>17</span> <span>if</span>(!<span>$this</span>->options["showHide"] && <span>substr</span>(<span>$dir</span>, 0, 1) == "."<span>){ </span><span>18</span> <span>continue</span><span>; </span><span>19</span> <span> } </span><span>20</span> <span>$dir</span> = <span>$path</span>.DIRECTORY_SEPARATOR.<span>$dir</span><span>; </span><span>21</span> <span>if</span>(<span>is_dir</span>(<span>$dir</span><span>)){ </span><span>22</span> <span>$this</span>->createTree(<span>$dir</span>, <span>$level</span><span>); </span><span>23</span> } <span>elseif</span> (<span>is_file</span>(<span>$dir</span><span>)){ </span><span>24</span> <span>array_push</span>(<span>$files</span>, <span>$dir</span><span>); </span><span>25</span> <span> } </span><span>26</span> <span> } </span><span>27</span> <span>closedir</span>(<span>$dirHandle</span><span>); </span><span>28</span> <span>foreach</span> (<span>$files</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) { </span><span>29</span> <span>$this</span>->tree .= <span>str_repeat</span>(<span>$this</span>->options["padding"], <span>$level</span><span>) </span><span>30</span> .<span>$this</span>->options["filepre"<span>] </span><span>31</span> .<span>$this</span>->_basename(<span>$value</span><span>) </span><span>32</span> .<span>$this</span>->options["newline"<span>]; </span><span>33</span> <span> } </span><span>34</span> <span>return</span> <span>$this</span><span>; </span><span>35</span> }
3.2 显示目录树
<span>1</span> <span>/*</span><span>* </span><span>2</span> <span> * 显示目录树 </span><span>3</span> <span>*/</span> <span>4</span> <span>public</span> <span>function</span><span> showTree(){ </span><span>5</span> <span>echo</span> "<pre class="brush:php;toolbar:false">" <span>6</span> .<span>$this</span>-><span>tree </span><span>7</span> ."
3.3 下载目录树
<span>1</span> <span>/*</span><span>* </span><span>2</span> <span> * 下载目录树文件 </span><span>3</span> <span>*/</span> <span>4</span> <span>public</span> <span>function</span> downloadTree(<span>$name</span><span>){ </span><span>5</span> <span>header</span>("Content-type:text/plain"<span>); </span><span>6</span> <span>header</span>("Content-Disposition:attachment;filename={<span>$name</span>}.txt"<span>); </span><span>7</span> <span>echo</span> <span>$this</span>-><span>tree; </span><span>8</span> }
3.4 试验下
用下面的两端代码分别测试下:
<span>1</span> <span>$t</span> = <span>new</span> Dirtree(<span>array</span>("padding"=>" ","newline"=>"<br>"<span>)); </span><span>2</span> <span>$t</span>->createTree("D:\autoload")->showTree("tree");
上面这段代码会将目录结构信息输出到浏览器,就像图1那样:
图 1 输出目录结构到浏览器 图 2 下载目录树结构
<span>1</span> <span>$t</span> = <span>new</span> Dirtree(<span>array</span>("padding"=>" ","newline"=>"\r\n"<span>)); </span><span>2</span> <span>$t</span>->createTree("D:\autoload")->downloadTree("tree");
上面的代码执行后,浏览器会下载一个tree.txt文件,打开文件的信息如图2所示
四、 总结
一个生成目录树功能基本完成了,但是这个要是有时间的话还可以在扩展扩展,让他更友好的支持命令行模式。或者对输出的内容做做强化,让文件夹可以折叠(js实现)。
本文版权归作者iforever(luluyrt@163.com)所有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
以上就介绍了遍历生成目录树,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。