Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

php - Multidimensional Directory List with Recursive iterator

I am trying to get multi-dimensional array for directories formatted as below :

[
    {
        "text": "another_folder",
        "href": "gui/default/uploads/another_folder",
        "depth": 0
    },
    {
        "text": "subfold",
        "href": "gui/default/uploads/subfold",
        "depth": 0,
        "nodes": {
                   "text": "sub-subfold",
                   "href": "gui/default/uploads/subfold/sub-subfold",
                   "depth": 1,
                  }
    }
]

I want to use RecursiveIterators. What I have done so far is I am getting all directories listed in given path. I need to go inside to children which is where I stacked.

public function list_folders($folder_path='') {

    if(!$folder_path) $folder_path = $this->upl_path;

    $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($folder_path),
                RecursiveIteratorIterator::SELF_FIRST);
    $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
    $r = array();
    $counter = 0
    foreach ($iterator as $splFileInfo) {
      if($splFileInfo->isDir()) {
        $r[$counter] = array(
          'text' => $splFileInfo->getFilename(),
          'href' => str_replace('\','/',$splFileInfo->getPathname())
        );
        if(How to check if it has children) {
          $result[$counter] += array('nodes'=> CALL RECURSIVE HERE ? );
      }
      $counter++;
    }


    echo json_encode($r,JSON_PRETTY_PRINT);
}

I'd use any idea or help gladly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code was almost functional, but it was missing a few key points. I adapted your code so that it works, and added some comments that I hope will help you understand what you were missing:

class FolderListing
{

    public function list_folders($folder_path = '', $depth = 0)
    {

        if (!$folder_path) $folder_path = $this->upl_path;

        $iterator = new IteratorIterator(new DirectoryIterator($folder_path));

        $r = array();
        foreach ($iterator as $splFileInfo) {

            if ($splFileInfo->isDot()) {
                continue;
            }

            // we need to do this for both folders and files
            $info = array(
                'text' => $splFileInfo->getFilename(),
                'href' => str_replace('\', '/', $splFileInfo->getPathname()),
                'depth' => $depth
            );

            // is we have a directory, try and get its children
            if ($splFileInfo->isDir()) {
                // !!! I recommend to do an echo $splFileInfo->getPathname() here
                // to see the order in which recursion works !!!
                $nodes = $this->list_folders($splFileInfo->getPathname(), $depth + 1);

                // only add the nodes if we have some
                if (!empty($nodes)) {
                    $info['nodes'] = $nodes;
                }
            }
            // add the info to the array. No need for a counter :)
            $r[] = $info;
        }

        // the return is important to be able to build the multi dimensional array
        return $r;
    }
}

$test = new FolderListing();
$ret = $test->list_folders('./test'); // change this to whatever you want
var_dump($ret);

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...