VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 分享一个生成文件层级树类

根据 php 递归读取文件夹生成文件树

  1. class Tree 
  2.  
  3. { 
  4.  
  5.     public $arr = array(); 
  6.  
  7.     public $icon = array( 
  8.  
  9.         '│', 
  10.  
  11.         '├─', 
  12.  
  13.         '└─' 
  14.  
  15.     ); 
  16.  
  17.     public $ret; 
  18.  
  19.     public function set_tree($arr = array()) 
  20.  
  21.     { 
  22.  
  23.         $this->arr = $arr; 
  24.  
  25.     } 
  26.  
  27.     public function get_child($myid) 
  28.  
  29.     { 
  30.  
  31.         $newarr = array(); 
  32.  
  33.         if (is_array($this->arr)) { 
  34.  
  35.             foreach ($this->arr as $id => $a) { 
  36.  
  37.                 if ($a['pid'] == $myid) { 
  38.  
  39.                     $newarr[$id] = $a; 
  40.  
  41.                 } 
  42.  
  43.             } 
  44.  
  45.         } 
  46.  
  47.         return $newarr ? $newarr : false; 
  48.  
  49.     } 
  50.  
  51.     //获取带格式数组 
  52.  
  53.     public function getArray($myid = 0, $sid = 0, $adds = '') 
  54.  
  55.     { 
  56.  
  57.         $number = 1; 
  58.  
  59.         $child = $this->get_child($myid); 
  60.  
  61.         if (is_array($child)) { 
  62.  
  63.             $total = count($child); 
  64.  
  65.             foreach ($child as $a) { 
  66.  
  67.                 $j = $k = ''; 
  68.  
  69.                 if ($number == $total) { 
  70.  
  71.                     $j .= $this->icon[2]; 
  72.  
  73.                 } else { 
  74.  
  75.                     $j .= $this->icon[1]; 
  76.  
  77.                     $k = $adds ? $this->icon[0] : ''; 
  78.  
  79.                 } 
  80.  
  81.                 $spacer = $adds ? $adds . $j : ''; 
  82.  
  83.                 $a['name'] = $spacer . ' ' . $a['name']; 
  84.  
  85.                 $this->ret[] = $a; 
  86.  
  87.                 $fd = $adds . $k . '   '; 
  88.  
  89.                 $this->getArray($a['id'], $sid, $fd); 
  90.  
  91.                 $number++; 
  92.  
  93.             } 
  94.  
  95.         } 
  96.  
  97.         return $this->ret; 
  98.  
  99.     } 
  100.  
  101.     //select 
  102.  
  103.     public function get_tree($myid, $str, $sid = 0, $adds = '') 
  104.  
  105.     { 
  106.  
  107.         $number = 1; 
  108.  
  109.         $child = $this->get_child($myid); 
  110.  
  111.         if (is_array($child)) { 
  112.  
  113.             $total = count($child); 
  114.  
  115.             foreach ($child as $a) { 
  116.  
  117.                 $id = $a['id']; 
  118.  
  119.                 $j = $k = ''; 
  120.  
  121.                 if ($number == $total) { 
  122.  
  123.                     $j .= $this->icon [2]; 
  124.  
  125.                 } else { 
  126.  
  127.                     $j .= $this->icon [1]; 
  128.  
  129.                     $k = $adds ? $this->icon [0] : ''; 
  130.  
  131.                 } 
  132.  
  133.                 $spacer = $adds ? $adds . $j : ''; 
  134.  
  135.                 $select = $id == $sid ? 'selected' : ''; 
  136.  
  137.                 $this->ret .= sprintf($str, $id, $select, $spacer, $a['name']); 
  138.  
  139.                 $this->get_tree($id, $str, $sid, $adds . $k . ' '); 
  140.  
  141.                 $number++; 
  142.  
  143.             } 
  144.  
  145.         } 
  146.  
  147.         return $this->ret; 
  148.  
  149.     } 
  150.  
  151.     //文件夹目录 
  152.  
  153.     public function read_all_dir($dir, $onlyDir = true, $ignore = []) 
  154.  
  155.     { 
  156.  
  157.         $result = array(); 
  158.  
  159.         $handle = opendir($dir); 
  160.  
  161.         if ($handle) { 
  162.  
  163.             while (($file = readdir($handle)) !== false) { 
  164.  
  165.                 if (in_array($file, $ignore)) continue; 
  166.  
  167.                 if ($file != '.' && $file != '..') { 
  168.  
  169.                     $cur_path = $dir . DIRECTORY_SEPARATOR . $file; 
  170.  
  171.                     if (is_dir($cur_path)) { 
  172.  
  173.                         $result[$file] = $this->read_all_dir($cur_path, $onlyDir); 
  174.  
  175.                     } else { 
  176.  
  177.                         if (!$onlyDir) { 
  178.  
  179.                             $result[] = $file; 
  180.  
  181.                         } 
  182.  
  183.                     } 
  184.  
  185.                 } 
  186.  
  187.             } 
  188.  
  189.             closedir($handle); 
  190.  
  191.         } 
  192.  
  193.         return $result; 
  194.  
  195.     } 
  196.  
  197.     //数组转换 
  198.  
  199.     public function arrshift($array, $pid = 0) 
  200.  
  201.     { 
  202.  
  203.         static $r = []; 
  204.  
  205.         static $index = 1; 
  206.  
  207.         if (is_array($array) && count($array) > 0) { 
  208.  
  209.             foreach ($array as $k => $v) { 
  210.  
  211.                 $r[] = array( 
  212.  
  213.                     'id' => $index, 
  214.  
  215.                     'pid' => $pid, 
  216.  
  217.                     'name' => is_array($v) ? $k : $v 
  218.  
  219.                 ); 
  220.  
  221.                 $index++; 
  222.  
  223.                 $this->arrshift($v, $index - 1); 
  224.  
  225.             } 
  226.  
  227.         } 
  228.  
  229.         return $r; 
  230.  
  231.     } 
  232.  
  233. } 

使用示例

  1. $tree = new Tree (); 
  2.  
  3. //文件夹遍历 
  4.  
  5. $data = $tree->read_all_dir(realpath('../file_dir'), false, ['.git', '.idea', 'vendor']); 
  6.  
  7. //转换成[['id','pid','name']]的二维数组 
  8.  
  9. $data = $tree->arrshift($data); 
  10.  
  11. $tree->set_tree($data);  
  12.  
  13. $data = $tree->getArray(); 
  14.  
  15. foreach ($data as $value) { 
  16.  
  17.     echo $value['name']; 
  18.  
  19.     echo '<br/>'; 
  20.  
  21.     echo '<br/>'; 
  22.  
  23. }

 

 
出处:http://www.phpfensi.com/php/20220620/21108.html

相关教程