Jump to content

Recommended Posts

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 !!

Link to comment
https://forums.phpfreaks.com/topic/171490-display-newest-image-in-directory/
Share on other sites

$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.

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)

$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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.