Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display Images From A Folder with PHP
//Display Images From A Folder with PHP
<?php
$files = glob("images/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;";
}
?>
//Display Images With Image Name From A Folder
<?php
$files = glob("images/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
print $num."<br />";
echo '<img src="'.$num.'" alt="random image" />'."<br /><br />";
}
?>
//Using lightbox
<div id="vlightbox1">
<?php
$thumbs = glob("data/facepainting_thumbs/*.*");
$images = glob("data/facepainting_images/*.*");
for ($i=1; $i<count($thumbs); $i++)
{
$numT = $thumbs[$i];
$numI = $images[$i];
echo '<a class="vlightbox1" href="'.$numI.'" title="'.$i.'"><img src="'.$numT.'"/></a>';
}
?>
</div>
@rrajkumar991197
Copy link

rrajkumar991197 commented Dec 15, 2017

thanks bro

@malikkurosaki
Copy link

malikkurosaki commented Feb 7, 2018

wonderfull thank

@sheehanmedia
Copy link

sheehanmedia commented Mar 13, 2018

Awesome snippets, but there's an error in the fromfolder.php example. Because an array index starts at zero, so should the for loop. Should update that line to:

for ($i=0; $i<count($files); $i++)

Hope this is helpful!

@Helpmetoday
Copy link

Helpmetoday commented Apr 15, 2018

I would like to do something very specific, and I think you can help me...

I want to display one random png image at a time from 250 images total. The images must not be displayed until 12-midnight and stay on display until 12-midnight the next day before the next image replaces the previous image. Can someone make that script? I want to use php + mysql for the script. This script is what used to be called a picture of the day script.

@sasijarvis
Copy link

sasijarvis commented Aug 24, 2018

thanks man!! this worked well

@philg2018
Copy link

philg2018 commented Jun 1, 2019

This helped a LOT!! Thank you so much. I have a question though, if anyone can help? I want to display the name of the image but without the path. How can I do that?

@riki1972
Copy link

riki1972 commented Jun 21, 2019

Change this
print $num ."
";
with this
print basename($num) ."
";

@therealabaaskills
Copy link

therealabaaskills commented Jan 6, 2021

Amazing, thanks.

I wish to see how to display img_name also 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment