Display All Image From Directory In Reverse Order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shawnthegrom
Forum Newbie
Posts: 3
Joined: Thu Mar 31, 2011 12:14 am

Display All Image From Directory In Reverse Order

Post by shawnthegrom »

Hi All,

I've been working at this for hours with no luck. I'm trying to display every image from a directory in a reverse order, essentially newest to oldest.
I've been successful in displaying all the images but they display oldest to newest. I read somewhere that an array reverse would do the trick but I'm kind of new to PHP!
Any help is greatly appreciated.

Here is the code I have:

Code: Select all

<?php
$files = glob("photobooth/*.*"); 
for ($i=0; $i<count($files); 
$i++) { $num = $files[$i]; 
echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;"; }
?>
Thanks!
jankidudel
Forum Commoner
Posts: 91
Joined: Sat Oct 16, 2010 4:30 pm
Location: Lithuania, Vilnius

Re: Display All Image From Directory In Reverse Order

Post by jankidudel »

couple ways to do that:

1. $files = array_reverse($files); foreach($files as $file)....

2. for(int $i = count($files) - 1; $i >= 0; $i--) ...
shawnthegrom
Forum Newbie
Posts: 3
Joined: Thu Mar 31, 2011 12:14 am

Re: Display All Image From Directory In Reverse Order

Post by shawnthegrom »

Thanks jankidudel but I'm not quite sure where to put that code for #1 into mine, and for #2 I keep getting a syntax error.
Could you show me where to put that array_reverse in my code? Also, don't I have to define the array first as well?

Shawn
shawnthegrom
Forum Newbie
Posts: 3
Joined: Thu Mar 31, 2011 12:14 am

Re: Display All Image From Directory In Reverse Order

Post by shawnthegrom »

Ok I figured it out. Here is what I did to reverse order the images and stop it from looping.

Code: Select all

<?php
$files = glob("photobooth/*.*"); 
$files = array_reverse($files);
foreach ($files as $file);
for ($i=0; $i<count($files); 
$i++) { $num = $files[$i]; 
echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;"; }
?>
Post Reply