Guillermo
Guillermo

Reputation: 326

Tree of directories and files

i trying to list directories and generate a tree. i have this function the but the problem is he give me the first empty array. when pass into easytreegrid give me an empty grid

    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;
}

give me this output:

[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
                                    )

                            )

                    )

            )

i want this output :

[{
"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"
    }]
}]

}]

Upvotes: 2

Views: 82

Answers (1)

Danila Ganchar
Danila Ganchar

Reputation: 11223

Example of function. Try this. Hope this help you.

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;
}

How to use:

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

Upvotes: 1

Related Questions