加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

介绍一个生成文件层级树类

发布时间:2022-07-29 14:10:16 所属栏目:PHP教程 来源:互联网
导读:根据 php 递归读取文件夹生成文件树 { public $arr = array(); public $icon = array( │, ├─, └─ ); public $ret; public function set_tree($arr = array()) { $this-arr = $arr; } public function get_child($myid) { $newarr = array(); if (is_arr
      根据 php 递归读取文件夹生成文件树
 
  {
 
      public $arr = array();
 
      public $icon = array(
 
          '│',
 
          '├─',
 
          '└─'
 
      );
 
      public $ret;
 
      public function set_tree($arr = array())
 
      {
 
          $this->arr = $arr;
 
      }
 
      public function get_child($myid)
 
      {
 
          $newarr = array();
 
          if (is_array($this->arr)) {
 
              foreach ($this->arr as $id => $a) {
 
                  if ($a['pid'] == $myid) {
 
                      $newarr[$id] = $a;
 
                  }
 
              }
 
          }
 
          return $newarr ? $newarr : false;
 
      }
 
      //获取带格式数组
 
      public function getArray($myid = 0, $sid = 0, $adds = '')
 
      {
 
          $number = 1;
 
          $child = $this->get_child($myid);
 
          if (is_array($child)) {
 
              $total = count($child);
 
              foreach ($child as $a) {
 
                  $j = $k = '';
 
                  if ($number == $total) {
 
                      $j .= $this->icon[2];
 
                  } else {
 
                      $j .= $this->icon[1];
 
                      $k = $adds ? $this->icon[0] : '';
 
                  }
 
                  $spacer = $adds ? $adds . $j : '';
 
                  $a['name'] = $spacer . ' ' . $a['name'];
 
                  $this->ret[] = $a;
 
                  $fd = $adds . $k . '   ';
 
                  $this->getArray($a['id'], $sid, $fd);
 
                  $number++;
 
              }
 
          }
 
          return $this->ret;
 
      }
 
      //select
 
      public function get_tree($myid, $str, $sid = 0, $adds = '')
 
      {
 
          $number = 1;
 
          $child = $this->get_child($myid);
 
          if (is_array($child)) {
 
              $total = count($child);
 
              foreach ($child as $a) {
 
                  $id = $a['id'];
 
                  $j = $k = '';
 
                  if ($number == $total) {
 
                      $j .= $this->icon [2];
 
                  } else {
 
                      $j .= $this->icon [1];
 
                      $k = $adds ? $this->icon [0] : '';
 
                  }
 
                  $spacer = $adds ? $adds . $j : '';
 
                  $select = $id == $sid ? 'selected' : '';
 
                  $this->ret .= sprintf($str, $id, $select, $spacer, $a['name']);
 
                  $this->get_tree($id, $str, $sid, $adds . $k . ' ');
 
      }
 
      //文件夹目录
 
      public function read_all_dir($dir, $onlyDir = true, $ignore = [])
 
      {
 
          $result = array();
 
          $handle = opendir($dir);
 
          if ($handle) {
 
              while (($file = readdir($handle)) !== false) {
 
                  if (in_array($file, $ignore)) continue;
 
                  if ($file != '.' && $file != '..') {
 
                      $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
 
                      if (is_dir($cur_path)) {
 
                          $result[$file] = $this->read_all_dir($cur_path, $onlyDir);
 
                      } else {
 
                          if (!$onlyDir) {
 
                              $result[] = $file;
 
                          }
 
                      }
 
                  }
 
              }
 
              closedir($handle);
 
          }
 
          return $result;
 
                      'name' => is_array($v) ? $k : $v
 
                  );
 
                  $index++;
 
                  $this->arrshift($v, $index - 1);
 
              }
 
          }
 
          return $r;
 
      }
 
  }
 
  使用示例
 
 
 
  $tree = new Tree ();
 
  //文件夹遍历
 
  $data = $tree->read_all_dir(realpath('../file_dir'), false, ['.git', '.idea', 'vendor']);
 
  //转换成[['id','pid','name']]的二维数组
 
  $data = $tree->arrshift($data);
 
  $tree->set_tree($data);
 
  $data = $tree->getArray();
 
  foreach ($data as $value) {
 
      echo $value['name'];
 
      echo '<br/>';
 
      echo '<br/>';

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读