Sort by upload date after a readdir

Asked

Viewed 272 times

1

I have this code that works fine, but I’d like the last images to be uploaded appear at the top of the page, instead of appearing in an order apparently 'Random'.

They are not in a database, they are only in a folder where they go using the function move_uploaded_file(pics/....).

$myDirectory = opendir("pics");
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

closedir($myDirectory);

$indexCount = count($dirArray);

for($index=0; $index < $indexCount; $index++) {
    $temp = explode('.', $dirArray[$index]);
    $extension = strtolower(end($temp));
    if ($extension == 'jpg' || $extension == 'png' || $extension == 'tif' || $extension == 'gif' || $extension == 'jpeg' || $extension == 'JPG'){ 
        echo '<a href="pics/' . $dirArray[$index] . '"><img class="image" src="pics/' . $dirArray[$index] . '" alt="Image"></a>';
    }
}

2 answers

2

You can use the function filemtime() to know the recording date of each image. Which can be used as key to sort the images, using uksort(), before they are processed.

See also:

Example:

<?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;
    }

    $imagens = glob($myDirectory."*.jpg");
    usort($imagens, "mtimecmp");
    array_reverse($imagens);

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

Soen source

0


With arsort, a dateint with the value generated by filemtime and with him the arsort simply sort the array that has been created ($img), the largest date being first.

<?php        
     $path = "pics/";
     $dir  = opendir($path);
     $img  = array();
     $exts = array('jpg', 'png', 'tif','gif'); //extensões aceitas
     if ($dir){
         while($arq = readdir($dir)){
             $ins = explode('.', $arq);
             $out = strtolower(end($ins));
             if ($arq != '.' && $arq != '..' && in_array($out, $exts)){
                $dateint = filemtime($path.$arq); 
                array_push($img, array(
                                 'dateint' => $dateint,
                                 'name' => $arq, 
                                 'date' => date('d/m/Y H:m:s', $dateint)));
             }
         }
         if ($img && sizeof($img) > 0){
             arsort($img);       
             foreach($img as $value){                
                 echo '<p>';
                 echo '<a href="pics/'.$value['name'].'">';
                 echo '<img class="image" src="pics/'.$value['name'].'" alt="Image">';
                 echo '</a>';
                 echo '</p>';
             }
         }
     }
  • This is what it is, but I wanted you to do the image display (last part of my code), and the last one uploaded would be at the top of the page

  • @Miguel, editing was done, with the change of viewing the images

  • Obgado, that was it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.