title: how to display folders and sub folders from dir in PHP
body: <p>I am trying to get a list with folders and sub folders i have the following that allows me to get the folders and sub folders but i needed it to be sorted out like the e.g below i have been trying but i dont know how i would get around. </p>
  
  <pre><code>Root/
  Root/Images
  Root/Images/UserImages
  
  Root/Text/
  Root/Text/User1/
  Root/Text/User1/Folder2
  </code></pre>
  
  <p>but at the monent its display like this</p>
  
  <pre><code>Root/css/
  tree/css/
  js/
  images/
  </code></pre>
  
  <p><strong>PHP CODE:</strong></p>
  
  <pre><code>    function ListFolder($path)
  {
  
      $dir_handle = @opendir($path) or die("Unable to open $path");
  
      //Leave only the lastest folder name
      $dirname = end(explode("/", $path));
  
      //display the target folder.
      echo ("$dirname/");
      while (false !== ($file = readdir($dir_handle)))
      {
          if($file!="." &amp;&amp; $file!="..")
          {
              if (is_dir($path."/".$file))
              {
                  //Display a list of sub folders.
                  ListFolder($path."/".$file);
                  echo "&lt;br&gt;";
              }
          }
      }
  
  
      //closing the directory
      closedir($dir_handle);
  }
  
      ListFolder("../");
  </code></pre>
  
  <p>Thank you</p>