Display Directory, subdirectory and its contents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepaks85
    New Member
    • Aug 2006
    • 114

    Display Directory, subdirectory and its contents

    Dear All,

    I know how to display contents of any directory. But what if I need to print the directory, subdirectory and its contents?

    The output should be:

    Folder
    Subfolder
    - File1
    - FIle2
    Subfolder2
    - File1
    - File2

    Please help me on this.

    Thanks

    Deepak
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You need to use recursion to go through directories. Have a look at glob() on php.net.

    Comment

    • nashruddin
      New Member
      • Jun 2008
      • 25

      #3
      This is a sample recursive function to display directory contents

      Code:
      <?php
      function scandir_recursive($dir) {
      	$items = scandir($dir);
      	
      	foreach($items as $item) {
      		if ($item == '.' || $item == '..') {
      			continue;
      		}
      		
      		$file = $dir . '/' . $item;
      		echo "$file<br />";
      		
      		if (is_dir($file)) {
      			scandir_recursive($file);
      		}
      	}
      }
      
      scandir_recursive('/home/nash');
      ?>

      Comment

      Working...