?
Solved

listing of directorys by data created

Posted on 2004-08-07
9
Medium Priority
?
280 Views
Last Modified: 2006-11-17
Hey there,

I have this code that returns the list of directorys inside a folder

$dir = 'path/';

if ($handle = opendir($dir)) {
  while (false !== ($dirs = readdir($handle))) {
    if ($dirs != "." && $dirs != ".." && is_dir($dir."/".$dirs)) {                  
      If ($ldirs) {
        array_push($ldirs, $dirs);  } else {      $ldirs = array($dirs);      }            
       }
    }
  closedir($handle);
}

And that works fine, I get a $ldirs array with the folder names in it. But what I would like is the array to be sorted with the newest folder at the top to the oldest folder at the bottom (by creation date)

Make sense?

Cheers
Nick

0
Comment
Question by:wildzero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 7

Accepted Solution

by:
zac_charles earned 600 total points
ID: 11745916
Hi,
This is done like this...

<?php
$dir = 'path/';//Define path (eg. C:/)

if ($handle = opendir($dir))
{

  while(false !== ($dirs = readdir($handle)))
  {
    if($dirs != "." && $dirs != ".." && is_dir($dir."/".$dirs))
    {
      if($ldirs)
      {
        array_push($ldirs, $dirs);//put folder name at the end of $ldirs (if $ldirs does exists)
      }
      else
      {
        $ldirs = array($dirs);//create ldirs with folder name in it (if $ldirs doesn't already exist)
      }
    }
  }
  closedir($handle);

}

$count = count($ldirs); //Count the number of folders in ldirs(int=>folder)
$times = array(); //Array to be populated by loop below (int=>time created)

for($i=0;$i<$count;$i++)//populate $times untill there is no more folders left to do
{
  $time = (time() - filectime("$dir/$ldirs[$i]"))/60/60/24;//define $time as the folders create time
  $times[$i] = $time;//add the time to the $times array
}

$cdirs = array_combine($ldirs,$times); //Make a new array (folder=>time created)
print_r($cdirs); //unsorted (folder=>time created)

array_multisort($cdirs, SORT_NUMERIC, SORT_DESC); //Sort by time created
print '<br><br>';

print_r($cdirs); //sorted (folder=>time created)
?>


Zac Charles
0
 
LVL 7

Expert Comment

by:zac_charles
ID: 11745924
By the way, if you sit there and refresh that page, you can see your folders growing older lol

Zac Charles
0
 
LVL 7

Expert Comment

by:zac_charles
ID: 11749871
I just tried this script on my other server, and it will only work on PHP5 because array_combine() is only in version 5. Here is a version that will use a less-then-php5 friendly bit of code in you dont have php5 and the array_combine() function if you do:

<?php
$dir = 'path/';//Define path (eg. C:/)

if ($handle = opendir($dir))
{

  while(false !== ($dirs = readdir($handle)))
  {
    if($dirs != "." && $dirs != ".." && is_dir($dir."/".$dirs))
    {
      if($ldirs)
      {
        array_push($ldirs, $dirs);//put folder name at the end of $ldirs (if $ldirs does exists)
      }
      else
      {
        $ldirs = array($dirs);//create ldirs with folder name in it (if $ldirs doesn't already exist)
      }
    }
  }
  closedir($handle);

}

$count = count($ldirs); //Count the number of folders in ldirs(int=>folder)
$times = array(); //Array to be populated by loop below (int=>time created)

for($i=0;$i<$count;$i++)//populate $times untill there is no more folders left to do
{
  $time = (time() - filectime("$dir/$ldirs[$i]"))/60/60/24;//define $time as the folders create time
  $times[$i] = $time;//add the time to the $times array
}

$phpVer = phpversion();

if($phpVer[1] != '5')//if you dont have php5
{
    $cdirs = array();
    for($i=0;$i<$count;$i++)
    {
        $cdirs[$ldirs[$i]] = $times[$i];
    }
}
else //if you have php5
{
    $cdirs = array_combine($ldirs,$times); //Make a new array (folder=>time created)
}

print_r($cdirs); //unsorted (folder=>time created)

array_multisort($cdirs, SORT_NUMERIC, SORT_DESC); //Sort by time created
print '<br><br>';

print_r($cdirs); //sorted (folder=>time created)
?>

There you go :)

Zac Charles
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 10

Author Comment

by:wildzero
ID: 11750438
Ah thats awesome, I actually read and tested the first code and found that it was only PHP5 so I whiped up a little arreycombine code lol. But yea thats perfect, just 1 question.

How I can I get the first folder name in the array? :)
Like, $cdirs[0] (which don't work I know, just exmaple)

But yea, thats perfect!
0
 
LVL 7

Expert Comment

by:zac_charles
ID: 11750784
The second one which will work for either php4 or PHP5 will make an array($cdirs) with the layout like this:
["folder_name"] => 'created_time'

I think that is the answer to your question, if not, can you please explain it better.

Zac Charles
0
 
LVL 10

Author Comment

by:wildzero
ID: 11751098
Yea thats cool
That last question, what I was after was
I get the array $cdirs

but I don't know what any of the folder_names are, and all I want is the first one at the top of the array... So how can I output the key of the first item at the top of the array?

Like array has
["folder"] => '10' and ["folder2"] => '12' and ["folder3"] => '14'

What I would like is to be-able to someway get that first folder name (first key in array) - ie folder
0
 
LVL 7

Expert Comment

by:zac_charles
ID: 11751175
foreach($cdirs as $dir=>$time)
{
  if(!$first)
  {
    $first = array("folder_name"=>$dir,"create_time"=>$time);
  }
}

print '['.$first["folder_name"].']';

Zac Charles
0
 
LVL 10

Author Comment

by:wildzero
ID: 11751180
Excellent
0
 
LVL 7

Expert Comment

by:zac_charles
ID: 11751191
Your welcome
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question