Sentinel47 Posted October 8, 2018 Share Posted October 8, 2018 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 comment Share on other sites More sharing options...
justsomeguy Posted October 8, 2018 Share Posted October 8, 2018 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. 1 Link to comment Share on other sites More sharing options...
Sentinel47 Posted October 9, 2018 Author Share Posted October 9, 2018 Thanks for your reply, Sorry for the confusion, but yeah I'm intending to use PHP to achieve this. Link to comment Share on other sites More sharing options...
iwato Posted October 9, 2018 Share Posted October 9, 2018 (edited) 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 October 9, 2018 by iwato Link to comment Share on other sites More sharing options...
justsomeguy Posted October 9, 2018 Share Posted October 9, 2018 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now