分享一个生成文件层级树类

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

  1. class Tree
  2. {
  3. public $arr = array();
  4. public $icon = array(
  5. '│',
  6. '├─',
  7. '└─'
  8. );
  9. public $ret;
  10. public function set_tree($arr = array())
  11. {
  12. $this->arr = $arr;
  13. }
  14. public function get_child($myid)
  15. {
  16. $newarr = array();
  17. if (is_array($this->arr)) {
  18. foreach ($this->arr as $id => $a) {
  19. if ($a['pid'] == $myid) {
  20. $newarr[$id] = $a;
  21. }
  22. }
  23. }
  24. return $newarr ? $newarr : false;
  25. }
  26. //获取带格式数组
  27. public function getArray($myid = 0, $sid = 0, $adds = '')
  28. {
  29. $number = 1;
  30. $child = $this->get_child($myid);
  31. if (is_array($child)) {
  32. $total = count($child);
  33. foreach ($child as $a) {
  34. $j = $k = '';
  35. if ($number == $total) {
  36. $j .= $this->icon[2];
  37. } else {
  38. $j .= $this->icon[1];
  39. $k = $adds ? $this->icon[0] : '';
  40. }
  41. $spacer = $adds ? $adds . $j : '';
  42. $a['name'] = $spacer . ' ' . $a['name'];
  43. $this->ret[] = $a;
  44. $fd = $adds . $k . '   ';
  45. $this->getArray($a['id'], $sid, $fd);
  46. $number++;
  47. }
  48. }
  49. return $this->ret;
  50. }
  51. //select
  52. public function get_tree($myid, $str, $sid = 0, $adds = '')
  53. {
  54. $number = 1;
  55. $child = $this->get_child($myid);
  56. if (is_array($child)) {
  57. $total = count($child);
  58. foreach ($child as $a) {
  59. $id = $a['id'];
  60. $j = $k = '';
  61. if ($number == $total) {
  62. $j .= $this->icon [2];
  63. } else {
  64. $j .= $this->icon [1];
  65. $k = $adds ? $this->icon [0] : '';
  66. }
  67. $spacer = $adds ? $adds . $j : '';
  68. $select = $id == $sid ? 'selected' : '';
  69. $this->ret .= sprintf($str, $id, $select, $spacer, $a['name']);
  70. $this->get_tree($id, $str, $sid, $adds . $k . ' ');
  71. $number++;
  72. }
  73. }
  74. return $this->ret;
  75. }
  76. //文件夹目录
  77. public function read_all_dir($dir, $onlyDir = true, $ignore = [])
  78. {
  79. $result = array();
  80. $handle = opendir($dir);
  81. if ($handle) {
  82. while (($file = readdir($handle)) !== false) {
  83. if (in_array($file, $ignore)) continue;
  84. if ($file != '.' && $file != '..') {
  85. $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
  86. if (is_dir($cur_path)) {
  87. $result[$file] = $this->read_all_dir($cur_path, $onlyDir);
  88. } else {
  89. if (!$onlyDir) {
  90. $result[] = $file;
  91. }
  92. }
  93. }
  94. }
  95. closedir($handle);
  96. }
  97. return $result;
  98. }
  99. //数组转换
  100. public function arrshift($array, $pid = 0)
  101. {
  102. static $r = [];
  103. static $index = 1;
  104. if (is_array($array) && count($array) > 0) {
  105. foreach ($array as $k => $v) {
  106. $r[] = array(
  107. 'id' => $index,
  108. 'pid' => $pid,
  109. 'name' => is_array($v) ? $k : $v
  110. );
  111. $index++;
  112. $this->arrshift($v, $index - 1);
  113. }
  114. }
  115. return $r;
  116. }
  117. }

使用示例

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