I've found this simple PHP script that takes images from a folder and list them in a page, the problem is that the images are listed alphabetically I think (which is fine, I use numbers at the end of the filename) but I want to to reverse the order, so instead of displaying filename1.jpg, filename2.jpg and so on it displays filename3.jpg, filename2.jpg, etc. <?php $folder = 'img/'; $filetype = '*.*'; $files = glob($folder.$filetype); echo '<table>'; for ($i=0; $i<count($files); $i++) { echo '<tr><td>'; echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>'; echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder)); echo '</td></tr>'; } echo '</table>'; ?> PHP: My knowledge is very limited to say the least but sounds very easy to do, if someone can help me out it would be awesome. Thanks
You have to reverse the for loop to start at the end of your array (count($files) - 1) rather than the beginning (0). <?php $folder = 'img/'; $filetype = '*.*'; $files = glob($folder.$filetype); echo '<table>'; for ($i = (count($files) - 1); $i >= 0; $i--) { echo '<tr><td>'; echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>'; echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder)); echo '</td></tr>'; } echo '</table>'; ?> PHP:
That worked perfectly! Thank you Iconiplex for the quick solution, it was much easier than I thought.
I'd just use an array sort... Though I'm wondering why if you only have one TD per TR why the blue blazes you're using a table... unless you're planning on adding information like filesize to it... much less why you'd output a numbered index that would invalidate every time the directory contents changed... or putting name attributes on the anchors like it's 1997... $files = arsort(glob($folder . $filetype)); poof, reverse order. Also, you might want to look at: http://us2.php.net/pathinfo Since this: substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder)); could be easier done as this: pathInfo($fileName, PATHINFO_BASENAME) 90% or more of the time you do brute-force string processing, particularly on things like filenames, the more likely it is there's already a function for that in PHP.