【问题标题】:Tree of directories and files目录和文件树
【发布时间】:2015-11-07 21:20:33
【问题描述】:

我试图列出目录并生成一棵树。 我有这个功能,但问题是他给了我第一个空数组。 当传入easytreegrid时给我一个空网格

    function listar_carpeta_archivo($dir = "C:/xampp/htdocs/Archivos") {

    $arr = array();
    foreach (new DirectoryIterator($dir) as $fileInfo) {

        if (!$fileInfo->isDot()) {
            $arr[]= $fileInfo->getBasename();
            if ($fileInfo->isDir()) {

               $arr[] = listar_carpeta_archivo($fileInfo->getPathname());

            }

        }

}

    return $arr;
}

给我这个输出:

[0] => Array
    (
    )

[1] => Array
    (
        [0] => 5962R1121310VXC
        [1] => Array
            (
                [0] => fdgtdfg
                [1] => Array
                    (
                        [0] => Ingreso
                        [1] => Array
                            (
                                [0] => 2015-08-10 15-13-17
                                [1] => Array
                                    (
                                        [0] => Nuevo documento de texto - copia (2).txt
                                        [1] => Nuevo documento de texto - copia (3).txt
                                        [2] => Nuevo documento de texto - copia.txt
                                        [3] => Nuevo documento de texto.txt
                                    )

                            )

                    )

            )

我想要这个输出:

[{
"id":1,
"name":"C",
"size":"",
"date":"02/19/2010",
"children":[{
    "id":2,
    "name":"Program Files",
    "size":"120 MB",
    "date":"03/20/2010",
    "children":[{
        "id":21,
        "name":"Java",
        "size":"",
        "date":"01/13/2010",
        "state":"closed",
        "children":[{
            "id":211,
            "name":"java.exe",
            "size":"142 KB",
            "date":"01/13/2010"
        },{
            "id":212,
            "name":"jawt.dll",
            "size":"5 KB",
            "date":"01/13/2010"
        }]
    },{
        "id":22,
        "name":"MySQL",
        "size":"",
        "date":"01/13/2010",
        "state":"closed",
        "children":[{
            "id":221,
            "name":"my.ini",
            "size":"10 KB",
            "date":"02/26/2009"
        },{
            "id":222,
            "name":"my-huge.ini",
            "size":"5 KB",
            "date":"02/26/2009"
        },{
            "id":223,
            "name":"my-large.ini",
            "size":"5 KB",
            "date":"02/26/2009"
        }]
    }]
},{
    "id":3,
    "name":"eclipse",
    "size":"",
    "date":"01/20/2010",
    "children":[{
        "id":31,
        "name":"eclipse.exe",
        "size":"56 KB",
        "date":"05/19/2009"
    },{
        "id":32,
        "name":"eclipse.ini",
        "size":"1 KB",
        "date":"04/20/2010"
    },{
        "id":33,
        "name":"notice.html",
        "size":"7 KB",
        "date":"03/17/2005"
    }]
}]

}]

【问题讨论】:

  • 首先你需要的是:检查$fileInfo->getBasename() 不为空。如果$fileInfo->getBasename() 不为空,则将此:if ($fileInfo->isDir()) { $arr[] = listar_carpeta_archivo($fileInfo->getPathname()); } 更改为:if ($fileInfo->isDir() && $fileInfo->getPathname() != null) { $arr[] = listar_carpeta_archivo($fileInfo->getPathname()); }
  • 同样的结果@DanilaGanchar
  • 相同的结果,因为您编辑了帖子。我还没有看到输出的例子。尝试答案中的功能。

标签: php arrays recursion data-structures directory


【解决方案1】:

函数示例。尝试这个。希望对您有所帮助。

function tree(DirectoryIterator $dir) {
    $data = array(
        'name' => $dir->getPathname(),
        'size' => $dir->getSize(),
        // etc...
    );

    foreach ($dir as $node) {
        if ($node->isDir() && !$node->isDot()) {
            // call recursion
            $data['children'][] = tree(new DirectoryIterator($node->getPathname()));
        } else if ($node->isFile()) {
            $data['children'][] = array(
                'name' => $node->getFilename(),
                'size' => $node->getSize(),
                // etc...
            );
        }
    }

    return $data;
}

使用方法:

$tree = tree(new DirectoryIterator('C:/xampp/htdocs/Archivos'));
echo '<pre>';
print_r($tree);
echo '</pre>';
die();

【讨论】:

    猜你喜欢
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多