peranha Posted April 2, 2009 Share Posted April 2, 2009 I want to open a directory, and get all the images in that folder and display them to the webpage No databse, just a directory of pics. Here is what I have. getimage.php page. <?php $path = "C:/pics"; //images them dir //$patha ="C:/pics"; $dh = opendir($path); while ($file = readdir($dh)) { if($file != "." && $file != "..") { $extension = substr($path, -3); if($extension == "jpg" || $extension == "jpeg"){ header("Content-type: image/jpeg"); }elseif($extension == "gif"){ header("Content-type: image/gif"); }elseif($extension == "bmp"){ header("Content-type: image/bmp"); } $path = "$path/$file"; readfile($path); } } closedir($dh); // close dir ?> Here is how it is called on the page. images1.php <?php echo "<img src='getimage.php' alt='' width=100 />"; ?> Currently it gets the first image in the directory, but every image after that, it adds the first image file name to the $path variable. example img1 = P3140722.JPG img2 = P3140722.JPG/P3140723.JPG img3 = P3140722.JPG/P3140723.JPG/P3140724.JPG etc.... Any ideas? Link to comment https://forums.phpfreaks.com/topic/152181-open-directory-get-images-and-display/ Share on other sites More sharing options...
justAnoob Posted April 2, 2009 Share Posted April 2, 2009 Here is something that I use to display all the pictures from a folder on my server. Hope this helps. Just change the $imgPath to what your folder configuration is, and change to picture extensions if you want <?php $imgPath = "userimages/Computers"; foreach(glob("$imgPath/{*.jpg,*.png,*.gif}", GLOB_BRACE) as $fname) { echo '<img src="' . $imgPath . '/' . baseName($fname) . '" width="150" border="1"><br/><br/>'; } ?> Link to comment https://forums.phpfreaks.com/topic/152181-open-directory-get-images-and-display/#findComment-799204 Share on other sites More sharing options...
Showcase Posted April 2, 2009 Share Posted April 2, 2009 While is a loop. You're storing $path/$file back into $path each time it loops back over but not resetting $path back to it's original value. Which explains why it just keeps adding $file to the end of $path. To fix this, either reset $path or create another variable for storing the full path: Resetting path: $path = "$path/$file"; readfile($path); $path = "C:/pics"; Using new variable: $full_path = "$path/$file"; readfile($full_path); Or don't even set $path in the while loop and put the $path/$file directly in readfile: replace: $path = "$path/$file"; readfile($path); with: readfile("$path/$file"); Link to comment https://forums.phpfreaks.com/topic/152181-open-directory-get-images-and-display/#findComment-799358 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.