一、序言
在寫上一篇部落格的時候,需要用到目錄樹結構來展示我的文件結構,於是乎吃力不討好的把所有文件夾和文件都手動的」遍歷「了一邊。後來我就想,這樣太容易出錯了,而且很費力氣,於是就想著寫個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> }View Code
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> .""; 8 } View Code
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> }View Code
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");View Code
上面這段程式碼會將目錄結構資訊輸出到瀏覽器,就像圖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");
View Code
上面的程式碼執行後,瀏覽器會下載一個tree.txt文件,開啟文件的資訊如圖2所示四、 總結
一個生成目錄樹功能基本上完成了,但是這個要是有時間的話還可以在擴展擴展,讓他更友好的支援命令列模式。或是對輸出的內容做做強化,讓資料夾可以折疊(js實作)。
本文版權歸作者iforever(luluyrt@163.com)所有,未經作者本人同意禁止任何形式的轉載,轉載文章之後必須在文章頁面明顯位置給出作者和原文連接,否則保留追究法律責任的權利。
以上就介紹了遍歷生成目錄樹,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。