walkblind Posted August 23, 2009 Share Posted August 23, 2009 I'm trying to work on a script that simply displays the newest image in a given "images" directory. I want it to be an include file so that when I place it in the body, the newest image is displayed. It would be nice if there was a NAVbar updating so that the images leading up to the newest update could also be viewed. Any pointers would be MUCH appreciated as it will probably shave a day off my efforts. Thank you !! Quote Link to comment https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/ Share on other sites More sharing options...
ignace Posted August 23, 2009 Share Posted August 23, 2009 $directoryPath = 'path/to/directory'; $files = scandir($directoryPath); $newest = 0; $newestImage = ''; foreach ($files as $file) { if ('.' === $file[0] || !preg_match_all('/\.(jpe?g|png|gif)$/', $file)) continue; $fullPath = implode(DIRECTORY_SEPARATOR, array($directoryPath, $file)); if (($ctime = filectime($fullPath)) > $newest) { $newest = $ctime; $newestImage = $fullPath; } } This gives you the time the $newest image was entered and the full path to the image in $newestImage. Quote Link to comment https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/#findComment-904340 Share on other sites More sharing options...
walkblind Posted August 23, 2009 Author Share Posted August 23, 2009 thanks for quick response. I've plugged in your solution but I get the following warning: Warning: preg_match_all() expects at least 3 parameters, 2 given in .../public_html/php/includes/newest.php on line 7 (newest.php being the name of the file i created with your code, of course) Quote Link to comment https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/#findComment-904345 Share on other sites More sharing options...
ignace Posted August 23, 2009 Share Posted August 23, 2009 $directoryPath = 'path/to/directory'; $files = scandir($directoryPath); $newest = 0; $newestImage = ''; foreach ($files as $file) { if ('.' === $file[0] || !preg_match('/\.(jpe?g|png|gif)$/', $file)) continue; $fullPath = implode(DIRECTORY_SEPARATOR, array($directoryPath, $file)); if (($ctime = filectime($fullPath)) > $newest) { $newest = $ctime; $newestImage = $fullPath; } } This gives you the time the $newest image was entered and the full path to the image in $newestImage. Fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/#findComment-904347 Share on other sites More sharing options...
walkblind Posted August 23, 2009 Author Share Posted August 23, 2009 still same error. i think your last post was a quote from earlier no? seems identical Quote Link to comment https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/#findComment-904349 Share on other sites More sharing options...
ignace Posted August 23, 2009 Share Posted August 23, 2009 still same error. i think your last post was a quote from earlier no? seems identical Can't be the same error I used a different function. And yes it's a quote but I fixed the code inside it. Quote Link to comment https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/#findComment-904354 Share on other sites More sharing options...
walkblind Posted August 23, 2009 Author Share Posted August 23, 2009 Sorry it was definitely an error in my implementation but it's now up and running smooooooothly. Thanks so much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/#findComment-904597 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.