PHP: How to populate a directory structure in an array

北城余情 提交于 2019-11-30 22:34:36
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach ($ritit as $splFileInfo) {
   $path = $splFileInfo->isDir()
         ? array($splFileInfo->getFilename() => array())
         : array($splFileInfo->getFilename());

   for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) {
       $path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path);
   }
   $r = array_merge_recursive($r, $path);
}

print_r($r);

This class to grab files and directories recursive (= incl. subdirs) by putting the paths in a single, usable array will give you a good headstart:

  class myRecursiveDirectoryParser
  {
     protected $currentPath;
     protected $slash;
     protected $rootPath;
     protected $recursiveTree;

     function __construct($rootPath,$win=false)
     {
        switch($win)
        {
           case true:
              $this->slash = '\\';
              break;
           default:
              $this->slash = '/';
        }
        $this->rootPath = $rootPath;
        $this->currentPath = $rootPath;
        $this->recursiveTree = array(dir($this->rootPath));
        $this->rewind();
     }

     function __destruct()
     {
        $this->close();
     }

     public function close()
     {
        while(true === ($d = array_pop($this->recursiveTree)))
        {
           $d->close();
        }
     }

     public function closeChildren()
     {
        while(count($this->recursiveTree)>1 && false !== ($d = array_pop($this->recursiveTree)))
        {
           $d->close();
           return true;
        }
        return false;
     }

     public function getRootPath()
     {
        if(isset($this->rootPath))
        {
           return $this->rootPath;
        }
        return false;
     }

     public function getCurrentPath()
     {
        if(isset($this->currentPath))
        {
           return $this->currentPath;
        }
        return false;
     }

     public function read()
     {
        while(count($this->recursiveTree)>0)
        {
           $d = end($this->recursiveTree);
           if((false !== ($entry = $d->read())))
           {
              if($entry!='.' && $entry!='..')
              {
                 $path = $d->path.$entry;

                 if(is_file($path))
                 {
                    return $path;
                 }
                 elseif(is_dir($path.$this->slash))
                 {
                    $this->currentPath = $path.$this->slash;
                    if($child = @dir($path.$this->slash))
                    {
                       $this->recursiveTree[] = $child;
                    }
                 }
              }
           }
           else
           {
              array_pop($this->recursiveTree)->close();
           }
        }
        return false;
     }

     public function rewind()
     {
        $this->closeChildren();
        $this->rewindCurrent();
     }

     public function rewindCurrent()
     {
        return end($this->recursiveTree)->rewind();
     }
  }

Next up comes an example of how to use the class. In your case, you would have to walk the data, use "explode('/')" in the loop and construct a new array as you've described above.

  $d = new myRecursiveDirectoryParser("./",false);
  echo($d->getRootPath() . "<br>");
  while (false !== ($entry = $d->read())) {
     echo($entry."<br>");
  }
  $d->close();

All you need to do is get busy. You're almost done when you take it from here. ;)

You could try this, where $dir is a specified path:

function dir_tree($dir) {
   $path = '';
   $stack[] = $dir;
   while ($stack) {
       $thisdir = array_pop($stack);
       if ($dircont = scandir($thisdir)) {
           $i=0;
           while (isset($dircont[$i])) {
               if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
                   $current_file = "{$thisdir}/{$dircont[$i]}";
                   if (is_file($current_file)) {
                       $path[] = "{$thisdir}/{$dircont[$i]}";
                   } elseif (is_dir($current_file)) {
                        $path[] = "{$thisdir}/{$dircont[$i]}";
                       $stack[] = $current_file;
                   }
               }
               $i++;
           }
       }
   }
   return $path;
}

I've made a gist of a build tree class. It uses RecursiveDirectoryIterator and produce an array or a JSON for ajax purpose.

Here it is: https://gist.github.com/jonataswalker/3c0c6b26eabb2e36bc90

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!