Solved

listing of directorys by data created

Posted on 2004-08-07
9
265 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

696 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