Solved

listing of directorys by data created

Posted on 2004-08-07
9
251 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

831 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