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 8851993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:17:33+00:00 2026-06-14T13:17:33+00:00

This code shows a list of folders. How can I sort these folders by

  • 0

This code shows a list of folders.

How can I sort these folders by time (last changed) ?

function folderlist(){
  $startdir = './';
  $ignoredDirectory[] = '.'; 
  $ignoredDirectory[] = '..';
   if (is_dir($startdir)){
       if ($dh = opendir($startdir)){
           while (($folder = readdir($dh)) !== false){
               if (!(array_search($folder,$ignoredDirectory) > -1)){
                 if (filetype($startdir . $folder) == "dir"){
                       $directorylist[$startdir . $folder]['name'] = $folder;
                       $directorylist[$startdir . $folder]['path'] = $startdir;
                   }
               }
           }
           closedir($dh);
       }
   }
return($directorylist);
}

$folders = folderlist();
    foreach ($folders as $folder){
    $path = $folder['path'];
    $name = $folder['name'];

echo '<li><a href="' .$path .'index.php?imgdir=' .$name . '" class="style1"><font face="Tahoma" class="ws8"><B>' . $name . '</B></a><br /></li>';
  }
?>
  • 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-14T13:17:34+00:00Added an answer on June 14, 2026 at 1:17 pm

    You must include filemtime() in your $directoryList[] and then sort your array by the mtime.

    function cmp_mtime_reverse($a, $b) {
        if ($a['mtime'] == $b['mtime'])
            return 0;
    
        if ($a['mtime'] > $b['mtime'])
            return -1;
    
        return 1;
    }
    
    function folderlist() {
        $startdir = './';
        $ignoredDirectory[] = '.'; 
        $ignoredDirectory[] = '..';
        if (is_dir($startdir)) {
            if ($dh = opendir($startdir)) {
                while (($folder = readdir($dh)) !== false) {
                    if (!(array_search($folder,$ignoredDirectory) > -1)) {
                        if (filetype($startdir . $folder) == "dir") {
                            $mtime = filemtime($startdir . $folder);
                            $directorylist[$startdir . $folder]['name'] = $folder;
                            $directorylist[$startdir . $folder]['path'] = $startdir;
                            $directorylist[$startdir . $folder]['mtime'] = $mtime;
                            $directorylist[$startdir . $folder]['files'] = count_files($startdir . $folder);
                        }
                    }
                }
    
                closedir($dh);
            }
        }
    
        uasort($directorylist, 'cmp_mtime_reverse');
        return $directorylist;
    }
    

    See Sorting Arrays and filemtime for more information.

    To count files inside a folder

    function count_files($startdir) {
        $ignoredDirectory[] = '.'; 
        $ignoredDirectory[] = '..';
        $n = 0;
        if ($dh = opendir($startdir)) {
            while (($file = readdir($dh)) !== false) {
                if (!(array_search($file, $ignoredDirectory) > -1)) {
                    $n++;
                }
            }
    
            closedir($dh);
        }
    
        return $n++;
    }
    

    and then add an entry in your function folderlist()

    $directorylist[$startdir . $folder]['files'] = count_files($startdir . $folder);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jquerymobile ,this code shows list with the colors of data-theme (black),
The error shows this code can not rollback: class AddCountToTag < ActiveRecord::Migration def change
I use below code to show folder list in AlertDialog: ListDialog = new AlertDialog.Builder(MyActivity.this);
This code shows syntax error in the parameter declaration.. Pls help me to solve
This code shows the structure of what I am trying to do. import multiprocessing
Why this code shows 0? Shouldn't it return 1? Messages = new Meteor.Collection(messages); if
Possible Duplicate: PHP: “Notice: Undefined variable” and “Notice: Undefined index” This code shows warnings
I have this code that shows all the images inside 'images' directory but it
This is my php code that connects me with another ftp server and shows
As I can't post code I'm asking this as a theoretical question, but giving

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.