Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

listing of directorys by data created

Posted on 2004-08-07
9
Medium Priority
?
283 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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

610 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