title: Count files and subfolders in directory
body: <p>I am writing a script which counts all files and folders recursively. I tried two approaches but each of these returns a different value:
  Script 1: 2376
  Script 2: 2178</p>
  <p>Here are the two scripts:</p>
  <pre><code>$source = 'folder';
  $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
  foreach($files as $file) {
      $result[] = $file.'&lt;br /&gt;';
  }
  echo count($result).'&lt;br /&gt;';
      function listFolders($dir) {
      if($dh = opendir($dir)) {
          $files = Array();
          $inner_files = Array();
          while($file = readdir($dh)) {
              if($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot;) {
                  if(is_dir($dir . &quot;/&quot; . $file)) {
                      $inner_files = listFolders($dir . &quot;/&quot; . $file);
                      if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                  } else {
                      array_push($files, $dir . &quot;/&quot; . $file);
                  }
              }
          }
          closedir($dh);
          return $files;
      }
  }
  foreach (listFolders($source) as $key=&gt;$file){
      $result2[] = $file .&quot;&lt;br /&gt;&quot;;
  }
  echo count($result2);
  </code></pre>
  <p>Is one of the scripts correct? What am I missing?</p>