Home >Backend Development >PHP Tutorial >Traverse the generated directory tree and generate the directory tree_PHP tutorial

Traverse the generated directory tree and generate the directory tree_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:191955browse

Traverse the generated directory tree and generate the directory tree

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 of 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, it will be called recursively. When encountering a file, it will be saved into an array first. , after traversing the current folder, splice the files in the array together. 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 the part Code:

  3.1 生成目录树

Traverse the generated directory tree and generate the directory tree_PHP tutorial 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 显示目录树

Traverse the generated directory tree and generate the directory tree_PHP tutorial1 /** 2 * 显示目录树 3 */ 4 public function showTree(){ 5 echo "
"
<span>6</span>              .<span>$this</span>-><span>tree
</span><span>7</span>              ."
"; 8 } View Code

 3.3 Download directory tree

Traverse the generated directory tree and generate the directory tree_PHP tutorial1 /** 2 * Download directory tree file 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 two ends of the code to test respectively:

Traverse the generated directory tree and generate the directory tree_PHP tutorial1 $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:

Traverse the generated directory tree and generate the directory tree_PHP tutorial1 $t = new Dirtree(array("padding"=>" ","newline "=>"rn")); 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

A directory tree generation function 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 consent of the author. After reprinting the article, the author and the original text link must be given in an obvious position on the article page, otherwise we will reserve the right to pursue it. Legal liability rights.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/937011.htmlTechArticleTraverse to generate a directory tree, generate a directory tree 1. Preface When writing the previous blog, you need to use the directory Tree structure to display my file structure, so it is a thankless task to put all files...
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