目录和文件树

时间:2015-08-14 15:01:42

标签: php arrays recursion data-structures directory

我试图列出目录并生成一棵树。 我有这个功能,但问题是他给了我第一个空数组。 当进入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"
    }]
}]

}]

1 个答案:

答案 0 :(得分: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();