Home  >  Article  >  Backend Development  >  Traverse the generated directory tree

Traverse the generated directory tree

WBOY
WBOYOriginal
2016-08-08 09:31:441624browse

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

 1     /**
 2      * 生成目录树
 3      */
 4     public function createTree($path, $level=0){
 5         $level       = $level;
 6         $this->tree .= str_repeat($this->options["padding"], $level)
 7                         .$this->options["dirpre"]
 8                        .$this->_basename($path)
 9                        .$this->options["newline"];
10         $level++;
11         $dirHandle  = opendir($path);
12         $files      = array();
13         while (false !== ($dir = readdir($dirHandle))) {
14             if($dir == "." || $dir == ".."){
15                 continue;
16             }
17             if(!$this->options["showHide"] && substr($dir, 0, 1) == "."){
18                 continue;
19             }
20             $dir     = $path.DIRECTORY_SEPARATOR.$dir;
21             if(is_dir($dir)){
22                $this->createTree($dir, $level);
23             } elseif (is_file($dir)){
24                 array_push($files, $dir);
25             }
26         }
27         closedir($dirHandle);
28         foreach ($files as $key => $value) {
29             $this->tree .= str_repeat($this->options["padding"], $level)
30                             .$this->options["filepre"]
31                             .$this->_basename($value)
32                               .$this->options["newline"];
33         }
34         return $this;
35     }
View Code

 3.2 Display directory tree

1     /**
2      * 显示目录树
3      */
4     public function showTree(){
5         echo "
"
6              .$this->tree
7              ."
"; 8 }
View Code

 3.3 Download directory tree

1     /**
2      * 下载目录树文件
3      */
4     public function downloadTree($name){
5         header("Content-type:text/plain");
6         header("Content-Disposition:attachment;filename={$name}.txt");
7         echo $this->tree;
8     }
View Code

 3.4 Under test

Use the following codes at both ends to test respectively:

1 $t = new Dirtree(array("padding"=>"    ","newline"=>"
")); 2 $t->createTree("D:\autoload")->showTree("tree");
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

1 $t = new Dirtree(array("padding"=>"    ","newline"=>"\r\n"));
2 $t->createTree("D:\autoload")->downloadTree("tree");
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.

Statement:
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