Steve1 Posted November 22, 2010 Share Posted November 22, 2010 The problem: I'm trying to create a page which outputs images from a folder which I have been able to do, but the problem I'm having is not being able to get the page to display the most recent image according to file modification date at the top. The first set of code below outputs the image timestamps in descending order, from newest to oldest which is what I want, but as soon as I change/add a couple lines of code (Shown in the second lot of code) to get the image file name along with the timestamp, the echoed list (timestamp and file names) gets muddled up in a random order. In short; As soon as the file names are retrieved with the timestamp, the list goes from being organised descendingly, to not. Show timestamp only code (1st lot of code): <?php //Open images directory $ignore = array("..","."); $dir = opendir("images1"); $images = array(); $sortedimages = array(); //List files in images directory while (($file = readdir($dir)) !== false) if (!in_array($file, $ignore)) $images[] = $file; foreach ($images as $image) { $filetime = filemtime("images1/$image"); $sortedimages[] = $filetime; } rsort($sortedimages); foreach ($sortedimages as $sorted) { //foreach ($sorted as $key => $value) //{ echo "$sorted<br/>"; } //} closedir($dir); ?> The show timestamp and file name code (2nd lot of code): <?php //Open images directory $ignore = array("..","."); $dir = opendir("images1"); $images = array(); $sortedimages = array(); //List files in images directory while (($file = readdir($dir)) !== false) if (!in_array($file, $ignore)) $images[] = $file; foreach ($images as $image) { $filetime = filemtime("images1/$image"); $sortedimages[] = array($filetime => $image); } rsort($sortedimages); foreach ($sortedimages as $sorted) { foreach ($sorted as $key => $value) { echo "$key and $value<br/>"; } } closedir($dir); ?> The changes made in the 2nd script from the 1st: //Changed: $sortedimages[] = $filetime; ---> $sortedimages[] = array($filetime => $image); //Included the previously commented out: foreach ($sorted as $key => $value) { } //Changed: echo "$sorted<br/>"; ---> echo "$key and $value<br/>"; Thanks for any help! Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 22, 2010 Share Posted November 22, 2010 I'm still looking thru your code to see if I can find answers to your specific questions. But in the meantime, I did notice a few things to comment on. 1) Is this code copy/pasted directly from your php file? If so, I don't see how it could actually be working. I don't see any curly braces following the if statement or the while statement. //List files in images directory while (($file = readdir($dir)) !== false) if (!in_array($file, $ignore)) 2) Rather than testing the current "file" to see if it's the current or parent folder, you could use the is_file function. Besides even if it's not the current or parent folder, that doesn't mean it's not a child folder. http://us.php.net/manual/en/function.is-file.php Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 22, 2010 Share Posted November 22, 2010 Try this on for size. This goes AFTER the while loop that creates the $images array (list of image filenames). <?php foreach ($images as $image) { $filetime = filemtime("images1/$image"); $sortedimages[]$filetime => $image; } krsort($sortedimages); echo "<pre>"; print_r($sortedimages); echo "</pre>" ?> Edit: the biggest change I made was to use krsort which is described at the following URL: http://us.php.net/manual/en/function.krsort.php Quote Link to comment Share on other sites More sharing options...
Steve1 Posted November 22, 2010 Author Share Posted November 22, 2010 Thanks for the help. If I enter the code that was provided, this error appears: Fatal error: Cannot use [] for reading "My/Directory/Here" on line 98. Line 98 is: $sortedimages[]$filetime => $image; I had a play about with it, but I just can't figure out what to do. Also, I did copy and paste the code above straight from my PHP file. Cheers Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 22, 2010 Share Posted November 22, 2010 Sorry. I missed an equal sign: $sortedimages[]$filetime => $image; I really don't see how the if & while statements can actually work as you've got them shown. Maybe someone else can explain that? Quote Link to comment Share on other sites More sharing options...
Steve1 Posted November 22, 2010 Author Share Posted November 22, 2010 I did try an equal sign so it read: []= but it still didn't work. This has really confused me and I don't know what to try next. Does anyone else have any suggestions? Thanks Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 22, 2010 Share Posted November 22, 2010 this is invalid syntax: $sortedimages[]$filetime => $image; maybe you mean: $sortedimages[$filetime] = $image; Quote Link to comment Share on other sites More sharing options...
Steve1 Posted November 23, 2010 Author Share Posted November 23, 2010 Thanks for the help, it's working now! I changed the first suggested bit of code to stop at krsort, as well as changing the $sortedimages as suggested. I then modified my existing code so I could get the timestamp and filename of the images into separate variables and it's now outputting exactly as I wanted, in descending order with newest image uploaded at the top: Such as; 1290370828 and House MD 1.jpg 1290370799 and Hugh Laurie - House.jpg 1290346142 and house.jpg Thanks. Quote Link to comment Share on other sites More sharing options...
AndrewFerrara Posted January 27, 2011 Share Posted January 27, 2011 Can u please post the ending script i've been trying to do the same thing for some images of mine.. <?php $dir = "./plate_cache"; $handle = opendir($dir); while (($file = readdir($handle))!==false) { if (strpos($file, '.png',1)) { echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a> "; } } closedir($handle); } ?> Quote Link to comment Share on other sites More sharing options...
doni49 Posted January 28, 2011 Share Posted January 28, 2011 Can u please post the ending script i've been trying to do the same thing for some images of mine.. <?php $dir = "./plate_cache"; $handle = opendir($dir); while (($file = readdir($handle))!==false) { if (strpos($file, '.png',1)) { echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a> "; } } closedir($handle); } ?> Andrew, Give us some info. [*]What is it you're trying to accomplish? [*]What is it doing that you don't want it to do? Or what is it NOT doing that you want it to do? [*]Are there any error messages? EDIT: You'd be better off starting a new thread to ask for help even if you want to reference this thread. This thread is marked as "Solved" so people who can help may not read this thread. The only reason I opened it is that I received an email messages from the system telling me that someone had replied to this thread. Quote Link to comment Share on other sites More sharing options...
AndrewFerrara Posted January 28, 2011 Share Posted January 28, 2011 From a directory "/plates-cache" display all .png images in the directory in descending order by their filetime 1. I am trying to display images on a html page 2. Display them in descending order by their filetime the script works fine for part 1 I have no idea how to do part 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.