0

I am using below code to generate photo gallery from a folder. How can i sort thumbnails date wise.

<?php

        /* settings */
        $image_dir = 'photo_gallery/';
        $per_column = 6;


        /* step one:  read directory, make array of files */
        if ($handle = opendir($image_dir)) {
            while (false !== ($file = readdir($handle))) 
            {
                if ($file != '.' && $file != '..') 
                {
                    if(strstr($file,'-thumb'))
                    {
                        $files[] = $file;
                    }
                }
            }
            closedir($handle);
        }

        /* step two: loop through, format gallery */
        if(count($files))
        {


            foreach($files as $file)
            {
                $count++;
                echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
                if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
            }
        }
        else
        {
            echo '<p>There are no images in this gallery.</p>';
        }

    ?>
jmj
  • 237,923
  • 42
  • 401
  • 438
Learner
  • 13
  • 4

1 Answers1

0

To hit your question directly, while you're reading the dir for files, you can get info about the files using some native php functions...

When a file was last accessed: fileatime - http://www.php.net/manual/en/function.fileatime.php

When a file was created: filectime - http://www.php.net/manual/en/function.filectime.php

When a file was modified: filemtime - http://php.net/manual/en/function.filemtime.php

These return the time, formatted as unix time.

For simplicity, I would use filectime to find the time, and use that value as the KEY in the $files array, like so: $files[filectime($file)] = $file;

Then you can use a simple array sorting function like ksort() to order them outside the loop, before you start step two.

Now... Going slightly deeper... I would probably use a database to store information like this, instead of hitting the file system every time the page is loaded. It will be a little more overhead in development, but depending on the size of the dir, could save you a lot of time and processing power.

TESTED 2012-06-23

    /* settings */
    $image_dir = 'photo_gallery/';
    $per_column = 6;


    /* step one:  read directory, make array of files */
    if ($handle = opendir($image_dir)) {
        while (false !== ($file = readdir($handle))) 
        {
            if ($file != '.' && $file != '..') 
            {
                if(strstr($file,'-thumb'))
                {
                    $files[filemtime($image_dir . $file)] = $file;
                }
            }
        }
        closedir($handle);
    }

    /* step two: loop through, format gallery */
    if(count($files))
    {
        krsort($files);

        foreach($files as $file)
        {
            $count++;
            echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
            if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
        }
    }
    else
    {
        echo '<p>There are no images in this gallery.</p>';
    }

?>

Ed Meacham
  • 543
  • 1
  • 5
  • 19
  • Thank you, by the way I used your suggestion but it was not working can you modify the code itself. used below code for reference: http://stackoverflow.com/questions/2667065/sort-files-by-date-in-php – Learner Jun 22 '12 at 13:29
  • Thank you. Modified code sorts files but last upload images goes to buttom insted of top (first images). could you please let me know what should i do to bring last uploaded image first. – Learner Jun 23 '12 at 16:53
  • Do you mean the order is reversed or that there is one specific image that is showing at the bottom and it should be at the top? – Ed Meacham Jun 23 '12 at 17:36
  • I understand now that I've seen the markup... the ksort function is sorting asc, you want desc... I have updated the code above to use krsort instead of ksort. – Ed Meacham Jun 23 '12 at 18:00