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?
Asked
Active
Viewed 45 times
0
-
4Welcome 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 Answers
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
-
Still having some issues, can you explain it a little more. Sorry for the inconvenience – BioHazard Apr 09 '16 at 23:48
-