get all file names from folder PHP

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

$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
";

$fileCount++;

}

}

if ($fileCount > 0) {

echo sprintf("
<h2>
All Files in %s></h2>

%sTotal Files: %s",$filePath,$string,$fileCount);

}

?>

Reference:http://www.snilesh.com

1 comment: