Jump to content

Getting the last 4 uploaded images from a directory

Recommended Posts

Hi all,

I'm trying to create a Carousel (with Bootstrap) that will obtain and display only the last 4 uploaded images from a specified directory (uploads/). I have a form on another page that allows user submission of an image, which then moves it to the uploads folder.

From there, I'm not sure how to begin writing the code to obtain the last 4 images from the directory.


Link to post
Share on other sites

Where and how have you stored the images?  In a folder or in a data base?  If in a folder does each image have a filename that reflects the order in which it was saved?  If in a data base, are you using a time stamp with each entry?


Edited by iwato
Link to post
Share on other sites

You can use a DirectoryIterator to loop through the files in the directory and save the filename and last modified date of each file in an array, then use something like asort to sort the array by the timestamp.


If you saved the array where the key is the filename, and the value is the timestamp, you can use asort to sort it in chronological order.


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...