title: How to get a list of files of subfolders and write them in a JSON using php?
body: <p>I'm already able to list all the file names and current directory/folder but I don't know how to create JSON entries for subdirectories </p>
  
  <p>Here is my code</p>
  
  <pre><code>&lt;?php
  $dir = "office/";
  if(is_dir($dir)){
      if($dh = opendir($dir)){
          while(($file = readdir($dh)) != false){
              if($file != "." and $file != ".."){
                  $files_array[] = array('file' =&gt; $file); // Add the file to the array
              } 
          }
      }
      $return_array =array('dir' =&gt; $files_array);
      exit (json_encode($return_array));
  }
  ?&gt;
  </code></pre>
  
  <p>and output is </p>
  
  <pre><code>{
      "dir": [
          {
              "file": "FreeWallpapersApp.zip"
          },
          {
              "file": "20151211_ClipArtForTextView.7z"
          },
          {
              "file": "QRite.7z"
          },
          {
              "file": "CustomDialog_app_contacts.zip"
          },
          {
              "file": "LockScreenBasicApp.apk"
          },
          {
              "file": "ImgViewEffects.zip"
          },
        ]
      }
  </code></pre>
  
  <p>How to show files inside subfolder using php to also generate file names which are in subdirectories .</p>