Phorce
Phorce

Reputation: 4642

PHP Recursive Function - Incrementing total values

I'm having this problem.

Let's assume that I have a series of folders, inside of these folders, they have have a unlimited supply of sub folders and you can have an unlimited supply of files within this.

Using Recusion, I am trying to get the number of all of the files that exist within the sub directories, as well as this, get the total number of files from the sub sub directly.

I am using the following:

$SubSectionTotalCount = 0;
$SubSectionComplete = 0;

function GetStats($child, $groups, $progress, $resCount, $complete)
{
    foreach($child->children as $childElement)
    {
          if($childElement->resources != null)
          {
              foreach($childElement->resources->groups as $groupss)
              {
                  if(Check($groupss->id, $groups))
                  {
                      if(array_key_exists($childElement->parent_id, $progress))
                      {
                          if(array_key_exists($childElement->resources->id, $progress[$childElement->parent_id]['tasks']))
                          {
                                $complete++;
                          }
                      }
                      $resCount++;
                      var_dump($resCount);
                  }
              }

          }
          GetStats($childElement, $groups, $progress, $resCount, $complete);

    }
}

I currently have 4 sections (which therefore resCount should print 4) but instead, I am getting:

int 1
int 2
int 3
int 2

If I don't increment the variables, and just var_dump("getting here") I get the following:

Getting here
Getting here
Getting here
Getting here 

So the recursion is working, however I don't understand why incrementing is not producing the desired output?

Upvotes: 1

Views: 348

Answers (2)

There are two commonly used methods for recursively walking directories in PHP. Primarily using either glob or RecursiveDirectoryIterator / RecursiveIteratorIterator.

Between the two, the iterator method is a more direct representation of file traversing. Try to get used to using iterators if you can.

Common Recursive Glob Method ( PHP 4+)

function getAllFiles($pattern) 
{
    $files = glob($pattern, 0); 
    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) 
        $files = array_merge($files, getAllFiles($dir.'/'.basename($pattern), 0));
    return $files;
}

$numFiles = count( getAllFiles('*') );

Common Recursive Iterator Method (PHP 5+)

$dirIterator = new RecursiveDirectoryIterator('/path');
$iterator = new RecursiveIteratorIterator(
                $dirIterator,
                RecursiveIteratorIterator::SELF_FIRST
);


$numFiles=0;
foreach ($iterator as $file)     ++$numFiles; //If you need to access files also.

$numFiles=iterator_count($iterator); //If you only want the count

Upvotes: 1

Davide Borsatto
Davide Borsatto

Reputation: 103

I'm not sure I'm reading you code correctly, but to me it seems like you're trying to modify a value inside a function, but then the value is not returned outside of it. Basically you should pass your parameter by reference (using &) instead of by value (which is what you're doing here), or a better option would be to refactor your code so that your function returns a value instead of trying to change one. That seems to be the problem to me.

Upvotes: 1

Related Questions