Listing Files or Directories with PHP



How to list files with PHP using the DirectoryIterator class.

<?php
// the folder to list
$images = 'images/';
// directory iterator
foreach (new DirectoryIterator($images) as $fileInfo) {
if($fileInfo->isDot()) continue;
// echo the image out and the filename
?>
<img src="<?php echo $images . $fileInfo->getFilename();?>" />
<br />
<?php echo $fileInfo->getFilename(); ?>
<?php
}
?>

Put this code in a .php file on your web server as a starting point for listing files. It will list any files and folders that aren't "Dot" files -- which are "." and "..", which relate to the current directory and the parent directory, respectively.