Solved

listing of directorys by data created

Posted on 2004-08-07
9
228 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
  • 6
  • 3
9 Comments
 
LVL 7

Accepted Solution

by:
zac_charles earned 150 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now