john_doemoor Posted September 10, 2012 Share Posted September 10, 2012 I have a problem, I would have to find a .csv file that is the most latest modified from a specific directory. And return the name of the latest modified file. Does some1 have a working example for this kind of case? Thank you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/268214-find-file-from-folder-with-latest-date-modified-stamp/ Share on other sites More sharing options...
Psycho Posted September 10, 2012 Share Posted September 10, 2012 It would be some pretty easy code to write. Do you have any experience with PHP? If not, then you should post in the freelance forum. If you do have some experience, give it a try then post back with any specific problems you have. Here is the process I would follow: 1. Create two variable to hold 1. the file namepath with the newest date and 2) the file's date. Set the first to false and the latter to 0. 2. Use glob() to get all the csv files from the specific directory into an array. 3. Loop through all files (using foreach) returned by glob. 4. Use filemtime() to get the last modified date of the file. 5. Test if that value is greater than the variable that holds the date of the recent file (created in step 1). If so, then set the two variables to the current file and date respectively. When the loop ends you will either have the most recent file and time in those two variables or (if there were no csv files) the file variable will be false. Quote Link to comment https://forums.phpfreaks.com/topic/268214-find-file-from-folder-with-latest-date-modified-stamp/#findComment-1376726 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.