PHP - Listing the contents of a directory

February 2017





With PHP, the contents of a directory and its subdirectories can be viewed. Here below is a recursive function, allowing you to browse directories and subdirectories and view the files:


function ScanDirectory($Directory){

  $MyDirectory = opendir($Directory) or die('Error');
	while($Entry = @readdir($MyDirectory)) {
		if(is_dir($Directory.'/'.$Entry)&& $Entry != '.' && $Entry != '..') {
                         echo '<ul>'.$Directory;
			ScanDirectory($Directory.'/'.$Entry);
                        echo '</ul>';
		}
		else {
			echo '<li>'.$Entry.'</li>';
                }
	}
  closedir($MyDirectory);
}

ScanDirectory('.');

Related


Published by netty5. Latest update on October 31, 2009 at 02:48 AM by jak58.
This document, titled "PHP - Listing the contents of a directory," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).