Page 1 of 1

Display All Image From Directory In Reverse Order

Posted: Thu Mar 31, 2011 12:20 am
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!

Re: Display All Image From Directory In Reverse Order

Posted: Thu Mar 31, 2011 10:33 am
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--) ...

Re: Display All Image From Directory In Reverse Order

Posted: Thu Mar 31, 2011 11:33 am
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

Re: Display All Image From Directory In Reverse Order

Posted: Thu Mar 31, 2011 1:08 pm
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;"; }
?>