Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8118649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:33:24+00:00 2026-06-06T04:33:24+00:00

I am using below code to generate photo gallery from a folder. How can

  • 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>';
        }

    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:33:25+00:00Added an answer on June 6, 2026 at 4:33 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

By using the code below in CodeIgniter , we can generate a table as
I wanted to generate a list view using below code. But after running this
Im trying to generate a color gradient using ColdFusion. My current code below works
In past, I am using Listview and using below code can show a particular
I am using below code to generate random numbers in range... int randomNumberWithinRange(int min,int
I am using below code to generate word document using assp.net/c#.net, please tell me
Hi i using this mysql code to generate this data below: code: SELECT L.LEAVETYPE,
I am trying to generate the below code via T4. using System; using MyDAL;
I'm trying to generate a fixed length hash using the code below. public int
I am trying to generate images using ImageMagick, please check the code below: <?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.