Home > Backend Development > PHP Tutorial > Traverse the generated directory tree

Traverse the generated directory tree

WBOY
Release: 2016-08-08 09:31:44
Original
1768 people have browsed it

1. Preface

When I was writing my last blog, I needed to use a directory tree structure to display my file structure, so I had to manually "traverse" all the folders and files. Later, I thought that this was too error-prone and very labor-intensive, so I thought about writing a php script to traverse the files and folders under a directory and generate a directory tree so that I can use the directory tree structure if needed in the future. Where, just run it directly. The directory tree structure currently generated by the script can be viewed directly through the browser, or downloaded to generate a txt file.

2. Introduction to ideas

 The idea of ​​generating a directory tree is very simple. Traverse the contents under the current folder and skip directly when encountering "." and "..". When encountering a folder, call it recursively. When encountering a file, save it to an array first, etc. After traversing the current folder, concatenate the files in the array. This operation is to generate the directory tree. After generation, there is another step to display or download the directory tree. There are still some details in the writing process, which will not be revealed until development. In order to make it easy to understand and expand, I put what can be done by a function into a class to make the idea of ​​traversing the folder clearer.

3. Code implementation

  Now that I have the idea, I feel comfortable writing code (this is also why good people often tell us that they even spend more time thinking about it when writing code, instead of writing code immediately). Let’s take a look at some of the code:

 3.1 Generate directory tree

<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>     }
Copy after login
View Code

 3.2 Display directory tree

<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 }
Copy after login
View Code

 3.3 Download directory tree

<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>     }
Copy after login
View Code

 3.4 Under test

Use the following codes at both ends to test respectively:

<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");
Copy after login
View Code

The above code will output the directory structure information to the browser, just like Figure 1:

                                                                                                   结 Figure 1 Output directory structure to browser Figure 2 download directory tree structure

<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");
Copy after login
View Code

After the above code is executed, the browser will download a tree.txt file, and the information about opening the file is shown in Figure 2 4. Summary

 The function of generating a directory tree is basically completed, but if you have time, you can expand it to make it more friendly and support the command line mode. Or enhance the output content so that the folder can be folded (js implementation).

 The copyright of this article belongs to the author iforever (luluyrt@163.com). Any form of reprinting is prohibited without the author's consent. After reprinting the article, the author and the original text link must be provided in an obvious position on the article page, otherwise the right to pursue legal liability is reserved. .

The above introduces the traversal to generate a directory tree, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template