1

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?

1
  • If you are recursing through directories you should take a look at some of PHP's built in classes, like RecursiveDirectoryIterator which helps with this sort of stuff. Commented Aug 10, 2015 at 15:31

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for this! I forgot that I could pass by reference over value.. How stupid but thank you :) -- Accepted answer
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.