2

I have the following php script display all images in a directory

<?php
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" height ="400"/><br />';
}
?>

I want to modify this so that when you visit the page it shows the last modified image on top. Could someone help me how I would go about doing that?

user1539206
  • 25
  • 1
  • 4

2 Answers2

2

You can use the filemtime() function to find the modification date of each file. This can be used as a key to sort the array using uksort() before it is processed in the loop.

This will put the array in ascending order of file modification time, i.e. those with the earliest mtime first. You can then either reverse the array, or iterate through it backwards.

<?php
    function mtimecmp($a, $b) {
        $mt_a = filemtime($a);
        $mt_b = filemtime($b);

        if ($mt_a == $mt_b)
            return 0;
        else if ($mt_a < $mt_b)
            return -1;
        else
            return 1;
    }

    $images = glob($dirname."*.jpg");
    usort($images, "mtimecmp");
    $images=array_reverse($images);

    foreach ($images as $image) {
        echo '<img src="'.$image.'" height ="400"/><br />';
    }
?>

(Iterating backwards is more efficient...)

    // ...
    usort($images, "mtimecmp");

    for ($i = count($images) - 1; $i >= 0; $i--) {
        $image = $images[$i];
        echo '<img src="'.$image.'" height ="400"/><br />';
    }
vanjavk
  • 117
  • 1
  • 10
Lucas Jones
  • 19,767
  • 8
  • 75
  • 88
  • passing filemtime() as the callback to uksort() won't work on its own. need a small utility function which interprets the result and returns an integer informing uksort(), ie: "Function cmp_function [the callback] should accept two parameters which will be filled by pairs of array keys. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second." – Nathan May 16 '13 at 23:38
  • @nathan: Absolutely right, here's me getting PHP and Python's use of 'key' in relation to sorting functions mixed up; in PHP, of course it refers to the keys of the array. Thanks. – Lucas Jones May 17 '13 at 00:14
  • Hi @LucasJones , your implementation worked perfectly! Is there anyway I can contact you directly for additional follow up? – user1539206 Jun 04 '13 at 16:58
0

You'll need to perform in two steps: (a) read the directory contents and note last modified info (b) render the result

$images = glob($dirname . '*.jpg');
$mostrecent = 0;
$mostrecentimg = null;

// scan
foreach ($images as $image) {
  $imagemod = filemtime($image);
  if ($mostrecent < $imagemod) {
    $mostrecentimg = $image;
    $mostrecent = $imagemod;
  }
}

// display
echo '<img src="' . $mostrecentimg . '" height="400"/><br />';
foreach($images as $image) {

  // the most recent was already output above so skip remainder this iteration
  if ($image == $mostrecentimg) continue;

  echo '<img src="' . $image . '" height="400"/><br />';
}
Nathan
  • 1,700
  • 12
  • 15