百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> 分享一个生成文件层级树类
分享文章到:

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

发布时间:09/01 来源:未知 浏览: 关键词:

按照 php 递归读取文件夹生成文件树

class Tree
{
    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 . ' ');
                $number++;
            }
        }
        return $this->ret;
    }
    //文件夹名目
    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;
    }
    //数组转换
    public function arrshift($array, $pid = 0)
    {
        static $r = [];
        static $index = 1;
        if (is_array($array) && count($array) > 0) {
            foreach ($array as $k => $v) {
                $r[] = array(
                    'id' => $index,
                    'pid' => $pid,
                    '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/>';
}

引荐教程:《PHP教程》

以上就是分享一个生成文件层级树类的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有154人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板