title: List all files and directories in JSON
body: <p>I am wanting away to list all my files and folders in a JSON but I need to be able to call the files using jQuery</p>
  
  <p>Basically what I have is the following</p>
  
  <pre><code>public function listproductimagedirectory($rootPath) {
      $pathStack = array($rootPath);
      $contentsRoot = array();
      $contents = &amp;$contentsRoot;
      while ($path = array_pop($pathStack)) {
         $contents[basename($path)] = array();
         $contents = &amp;$contents[basename($path)];
         foreach (scandir($path) as $filename) {
             if ('.' != substr($filename, 0, 1)) {
                 $newPath = $path.'/'.$filename;
                 if (is_dir($newPath)) {
                     array_push($pathStack, $newPath);
                     $contents[basename($newPath)] = array();
                 } else {
                     $contents[basename($filename)] = $newPath;
                 }
             }
         }
      }
      return $contentsRoot[basename($rootPath)];
      }
  </code></pre>
  
  <p>which outputs </p>
  
  <pre><code>{"giftcard":{"31791_319x242_0.jpg":"..\/products\/giftcard\/31791_319x242_0.jpg","7520921_0_9999_lge_v1_m56577569834701600.jpg":"..\/products\/giftcard\/7520921_0_9999_lge_v1_m56577569834701600.jpg","AMF.jpg":"..\/products\/giftcard\/AMF.jpg","accor.jpg":"..\/products\/giftcard\/accor.jpg","accorhotel.png":"..\/products\/giftcard\/accorhotel.png","cellarmasters.png":"..\/products\/giftcard\/cellarmasters.png","dusk.png":"..\/products\/giftcard\/dusk.png","giftca_100ath_lrg.png":"..\/products\/giftcard\/giftca_100ath_lrg.png","giftca_100goo_lrg.png":"..\/products\/giftcard\/giftca_100goo_lrg.png","giftca_100sun_lrg.png":"..\/products\/giftcard\/giftca_100sun_lrg.png","giftca_20itu_lrg.png":"..\/products\/giftcard\/giftca_20itu_lrg.png","giftca_25abc_lrg.png":"..\/products\/giftcard\/giftca_25abc_lrg.png","giftca_25jay_lrg.png":"..\/products\/giftcard\/giftca_25jay_lrg.png","giftca_30bra_lrg.png":"..\/products\/giftcard\/giftca_30bra_lrg.png","giftca_30div_lrg.png":"..\/products\/giftcard\/giftca_30div_lrg.png","giftca_40ebg_lrg.png":"..\/products\/giftcard\/giftca_40ebg_lrg.png","giftca_50col_lrg.png":"..\/products\/giftcard\/giftca_50col_lrg.png","giftca_50cou_lrg.png":"..\/products\/giftcard\/giftca_50cou_lrg.png","giftca_50dot_lrg.png":"..\/products\/giftcard\/giftca_50dot_lrg.png","giftca_50dym_lrg.png":"..\/products\/giftcard\/giftca_50dym_lrg.png","giftca_50hoy_lrg.png":"..\/products\/giftcard\/giftca_50hoy_lrg.png","giftca_50itu_lrg.png":"..\/products\/giftcard\/giftca_50itu_lrg.png","giftca_50jac_lrg.png":"..\/products\/giftcard\/giftca_50jac_lrg.png","giftca_50jea_lrg.png":"..\/products\/giftcard\/giftca_50jea_lrg.png","giftca_50pet_lrg.png":"..\/products\/giftcard\/giftca_50pet_lrg.png","giftca_50por_lrg.png":"..\/products\/giftcard\/giftca_50por_lrg.png","giftca_50reb_lrg.png":"..\/products\/giftcard\/giftca_50reb_lrg.png","giftca_50san_lrg.png":"..\/products\/giftcard\/giftca_50san_lrg.png","isubscribe.jpg":"..\/products\/giftcard\/isubscribe.jpg","isubscribe.png":"..\/products\/giftcard\/isubscribe.png","peteralexander.png":"..\/products\/giftcard\/peteralexander.png","rydges hotel.png":"..\/products\/giftcard\/rydges hotel.png","smiggle.png":"..\/products\/giftcard\/smiggle.png","sportsgirl.png":"..\/products\/giftcard\/sportsgirl.png","sussan.png":"..\/products\/giftcard\/sussan.png","wiggles.png":"..\/products\/giftcard\/wiggles.png"}}
  </code></pre>
  
  <p>What I see and I could be wrong but there is nothing the allows me to tell the ID of the array for e.g I am use to calling a JSON by using the each function and using something like this</p>
  
  <p><code>obj.title</code> that would tell me that its the title of the json row.</p>