title: PHP file tree - Listing Total Files
body: <h2>PHP Script</h2>
  
  <pre><code>&lt;?php
      echo '&lt;ul class="DirView"&gt;&lt;li&gt;&lt;a href="#"&gt;Recently Used&lt;span&gt;28&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;';
      $path = "../Desktop/IMG/BananzaNews/";
      $dir = new DirectoryIterator($path);
      foreach ($dir as $fileinfo) {
          if ($fileinfo-&gt;isDir() &amp;&amp; !$fileinfo-&gt;isDot()) {
              $subPath = $path.$fileinfo-&gt;getFilename();
              $subDir = new DirectoryIterator($subPath);
              $count = count(glob($subPath. "/*.*"));
              echo '&lt;li&gt;&lt;a href="#"&gt;'.$fileinfo-&gt;getFilename().'&lt;span&gt;'.$count.'&lt;/span&gt;&lt;/a&gt;&lt;ul class="sub-menu"&gt;';
              foreach ($subDir as $subPath) {
                  if ($subPath-&gt;isDir() &amp;&amp; !$subPath-&gt;isDot()) {
                      $file = $subPath-&gt;getFilename();
                      $fullPath = $path.$subPath.'/'.$file.'/*.*';
                      $inFullPath = count(glob($fullPath. '/*.*'));
                      echo '&lt;li&gt;&lt;a href="#"&gt;'.$file.'&lt;span&gt;44&lt;/span&gt;&lt;/a&gt;&lt;li&gt;';
                  }
              }
              echo '&lt;/ul&gt;&lt;/li&gt;';
          }
      }
      echo '&lt;/ul&gt;';
  ?&gt;
  </code></pre>
  
  <h2>Result</h2>
  
  <p><img src="https://i.stack.imgur.com/iI9wg.png" alt="My PHP File Tree Preview"></p>
  
  <h2>The Problem</h2>
  
  <p>Only ignoring the 'recently used' category, the rest fully reads my directories of which are duplicated for testing purposes. As you can see, each sub_category has 44 files inside.</p>
  
  <p>What I now want to do is count the total files (44 + 44) and display them on their parent category (88) however as I've already written this to my page, is this possible or is a bit of re-scripting needed?</p>