PHP: how to populate the directory structure in an array

I am creating an admin panel that shows the directory structure of a specific location on the server. I have a recursive php function that iterates through each file and folder. I cannot figure out how to save this directory structure in an associative php array as follows:

array[foldername1][0]=file; // if the foldername1 contains a file array[foldername1][foldername2][0]=file //if foldername1 contains another folder(foldername2) along with the file. 

The rule I'm trying to follow; the folder should always be the key, and the file should always have the following index:

 array[folder1][folder2][0]=file1; array[folder1][folder2][1]=file2; 

The fill function of this associative array should be general, since we never know what the directory structure is. I want json_encode to return this array to my client and process it in javascript, which is not a problem at the moment.

If this is a bad approach, please let me know what might be the best way to do this. I thought about using a flat array, but I think that it is a poor design.

+3
source share
4 answers
 $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); 
+6
source

This class, to capture files and directories recursively (= including subdirs), putting the paths in one useful array, will give you a good hat:

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

The following is an example of using the class. In your case, you will need to go through the data, use "explode ('/')" in the loop and build a new array, as you described above.

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

All you have to do is do it. You are almost done when you take it from here .;)

+2
source

You can try this where $ dir is the 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; } 
+2
source

I created an assembly tree class entity. It uses a RecursiveDirectoryIterator and creates an array or JSON for the ajax target.

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

0
source

All Articles