Display the latest images in a directory php -


i've had can find how display latest image, or display them all. need display 5 latest images.

my current code display 1 image is

<?php $dir = 'images/other'; $base_url = '/images/other'; $newest_mtime = 0; if ($handle = opendir($dir)) {     while (false !== ($file = readdir($handle))) {         if (($file != '.') && ($file != '..')) {             $mtime = filemtime("$dir/$file");             if ($mtime > $newest_mtime) {                 $newest_mtime = $mtime;                 $show_file = "$base_url/$file";             }         }     } } print '<img src="' .$show_file. '" alt="code">'; ?> 

this should work you:

(first files glob() , sort them last modification filemtime() , usort(). after 5 newest array_slice(). , @ end loop through them , print images)

<?php      $dir = "images/other";      $files = glob($dir . "/*.*");     usort($files, function($a, $b){         return (filemtime($a) < filemtime($b));     });      $files = array_slice($files, 0, 5);      foreach($files $file)         echo "<img src='" . $file. "' alt='code'>";  ?> 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -