Jump to content

[SOLVED] Reverse order of directory listing produced by code..


cgm225

Recommended Posts

I am using the following code to list the subdirectories under a main directory.  However, I want to reverse the order that they are currently listing in.  Preferably, I would like them to list based on each sub-directory's last modification time, with the most recently modified directories at the top of the list.

 

Thank you all in advance!

 

<?php
if ($handle = opendir('/some_directory')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file\n";
        }
    }
    closedir($handle);
}
?> 

try something like this

 

<?php
if ($handle = opendir('/some_directory')) {
$i = 0;    
while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
          $files[$file]['mdate'] = filemtime($filename);
          $files[$file]['name'] = $file; 
        }
    }
    closedir($handle);
array_multisort($files[]['mdate']);  //check this it might be wrong, but it should get you it all sorted by 'mdate' and then you can just reverse it if needed
}
?> 

That is very helpful!  Thank you

 

However, I am having some trouble getting the array_multisort function to work.. could you help me with that?  I am getting a "Fatal error: Cannot use [] for reading in /root/testing.php on line 19" error.

<?php
if ($handle = opendir('/some_dir/')) {
    while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
        $file1[] = $file;
      }
    }
    closedir($handle);
}

    foreach(array_reverse($file1) as $file){
      echo $file."<br>";
    }
?> 

JP the readdir does:

Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.

 

it will not sort using that method

 

I'm trying a blank and i can't remember how to sort $files using the inner array values

try $sortted = arsort($files[]['mdate']);

 

 

Yes, I know that it sorts them how they were stored... but if he got them that way in the first place, and then wanted those to be reversed, it would work his way.

 

 

I read your sig... and saw

 

Expert in mutli-d array analysis and mathematical modeling of real world applications using partial differential equations and integral calculus.

 

lol, I am still a noob with arrays, and learning alot.. I know some stuff about multi D arrays, but thats with Java... but kinda the same thing

 

 

fyi that isn't rock solid because of the function readdir, but if you don't do anything stupid to the files it won't hurt you.  (like if you drag around files in the ftp or resort them in a method other than date the readdir will read based on a different sort)

my hardest part is deciding on how high up i make my primary key (so to speak)  most of the time I like to make a primary key in my  3rd or high power level to it and I use a generic name then I use my first search parm, p-key, more search parm and then all the way down the data I need.  I made a search engine that does 5 level multi d sort of data for some SEO help and paganiation usages and of course to display the end result data.

 

arrays are really powerful, its just tricky to see them sometimes because of their abstract concept

well your so called counting index doesn't need to be your first key it can be a buried key which i do a lot of times for an optimization of sorting.  I like to grab more data than what's needed in a mysql query and then sort it out in "post" via arrays and logic, if i could i run 1 mysql query on each page, but sometimes you need that second.  Rarely do i ever need more than 2 queries to get data from the tables.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.