Steve
Steve

Reputation: 1211

Recursive function scanning a folder and storing it in an array

I am trying to make a recursive function to go through all of the folder path that I have given it in the parameters.

What I am trying to do is to store the folder tree into an array for example I have Folder1 and this folder contains 4 text files and another folder and I want the structure to be a multidimensional array like the following

Array 1 = Folder one
   Array 1 = text.text.....So on so forth

I have the following function that I build but its not working as I want it too. I know that I need to check whether it is in the root directory or not but when it becomes recursive it becoems harder

function displayAllFolders($root)
{
    $foldersArray = array();
    $listFolderFile = scandir($root);
    foreach($listFolderFile as $row)
    {
        if($row == "." || $row == "..")
        {
            continue;
        }
        elseif(is_dir("$root/$row") == true)
        {
            $foldersArray["$root/$row"] = "$row";
            $folder = "$root/$row";
            #$foldersArray[] = displayAllFolders("$root/$row");
        }
        else
        {
            $foldersArray[]= array("$root/$row") ;
        }
    }
        var_dump($foldersArray);
}

Upvotes: 0

Views: 74

Answers (3)

Yoshi
Yoshi

Reputation: 54649

Using RecursiveDirectoryIterator with RecursiveIteratorIterator this becomes rather easy, e.g.:

$it = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator(
    // root dir
    '.',

    // ignore dots
    RecursiveDirectoryIterator::SKIP_DOTS
  ),

  // include directories
  RecursiveIteratorIterator::SELF_FIRST

  // default is:
  //   RecursiveIteratorIterator::LEAVES_ONLY
  //
  // which would only list files
);

foreach ($it as $entry) {
  /* @var $entry \SplFileInfo */
  echo $entry->getPathname(), "\n";
}

Upvotes: 3

Marcel
Marcel

Reputation: 5119

Why not using PHP itself? Just have a look at the RecursiveDirectoryIterator of the standard php library (SPL).

$folders = [];
$iterator = new RecursiveDirectoryIterator(new RecursiveDirectoryIterator($directory));
iterator_apply($iterator, 'scanFolders', array($iterator, $folders));

function scanFolders($iterator, $folders) {
    while ($iterator->valid()) {
        if ($iterator->hasChildren()) {
            scanFolders($iterator->getChildren(), $folders);
        } else {
            $folders[] = $iterator->current();
        }
        $iterator->next();
    }
}

Upvotes: 0

user2316338
user2316338

Reputation: 9

Your approach isn't recursive at all.

It would be recursive if you called the same function again in case of a directory. You only make one sweep.

Have a look here:

http://php.net/manual/en/function.scandir.php

A few solutions are posted. I would advise you to start with the usercomment by mmda dot nl. (function is named dirToArray, exactly what you are tryting to do.) In case it will be removed, I pasted it here:

function dirToArray($dir) {  
   $result = array();
   $cdir = scandir($dir);
   foreach ($cdir as $key => $value) {
      if (!in_array($value,array(".","..")))  {
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value))  {
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
         }
         else {
            $result[] = $value;
         }
      }
   }  
   return $result;
} 

Upvotes: 0

Related Questions