HOW TO: Load and display images from a folder in PHP

If you have an unknown quantity of images in a folder and want to cycle through them and display (in a table or similar) then this snippet should get you started.


// path to images
$path = "./images/";
// Open images folder
$dir_handle = @opendir($path) or die("Cannot open folder");
// Loop through the fimages
while (false !== ($file = readdir($dir_handle))) {
// Prevent system files from being displayed
if($file == "." || $file == "..") continue;
// Output your html for displaying the images
echo trim("<img src="http://www.daryl.mu/wp-admin/$path$file" alt="$file" border="0" />");
// Loop ends
}
// Close directory
closedir($dir_handle);

1 Response to “HOW TO: Load and display images from a folder in PHP”


  1. 1 btp Jul 31st, 2009 at 1:58 am

    Thanks brah

Leave a Reply






Add to Technorati Favorites
Zen Handbook

Latest Posts

RSS