get all file names from folder PHP

Following code will retrieve all file names from a specified folder.

<?php
$filePath = "/var/www/public_html/mysite/images";/* Enter path to the folder */
$string="";
$fileCount=0;

$dir = opendir($filePath); 
while ($file = readdir($dir)) { 
  if (eregi("\.png",$file)) { /* Look for files with .png extension */
    $string .= "$file<br />";
    $fileCount++;
  }
}
if ($fileCount > 0) {
  echo sprintf("<h2>All Files in %s</h2><br />%s<strong>Total Files: %s</strong>",$filePath,$string,$fileCount);
}
?> 

Leave a Reply

Your email address will not be published. Required fields are marked *