Jump to content
Sentinel47

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.

 

Share this post


Link to post
Share on other sites

You won't be able to do that with just Javascript, you'll need a server-side language to read the directory contents and print the filenames into an array that you can use.

  • Confused 1

Share this post


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?

Roddy

Edited by iwato

Share this post


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.

http://php.net/manual/en/directoryiterator.getmtime.php

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.

http://php.net/manual/en/function.asort.php

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×