0

I'm trying to make a photo gallery on a website, I want it to display every photo from a certain directory. I figured a PHP loop work for this (don't really like Javascript). Is there a way I can show every picture from a directory inside my HTML code using PHP?

L777
  • 7,719
  • 3
  • 38
  • 63
BioHazard
  • 3
  • 1
  • 4
    Welcome to site! For basic questions like this, make sure to search first - you are pretty much guaranteed to find an answer: http://stackoverflow.com/questions/2922954/getting-the-names-of-all-files-in-a-directory-with-php – patricksweeney Apr 08 '16 at 19:07

1 Answers1

0

Try This code it worked for me

<?php
$imgdir = 'source/'; //Pick your folder
$allowed_types = array('png','jpg','jpeg','gif'); //Allowed types of files
$dimg = opendir($imgdir);//Open directory
while($imgfile = readdir($dimg))
{
  if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
      in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
/*If the file is an image add it to the array*/
  {$a_img[] = $imgfile;}
}
echo "<ul>";

 $totimg = count($a_img);  //The total count of all the images
//Echo out the images and their paths incased in an li.
 for($x=0; $x < $totimg; $x++){echo "<li><img src='" . $imgdir . $a_img[$x] . "' /></li>";}
echo "</ul>";
?>
MagnidownZ
  • 41
  • 7