cgm225 Posted July 8, 2007 Share Posted July 8, 2007 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); } ?> Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/ Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 Don't know if its possible off read, probably you would need to first gather all in some sort of multi D array and then sort by Modified date and then work off of that, the opendir only goes alphabetically I believe Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292307 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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 } ?> Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292309 Share on other sites More sharing options...
cgm225 Posted July 8, 2007 Author Share Posted July 8, 2007 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. Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292313 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 <?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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292315 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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']); Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292317 Share on other sites More sharing options...
cgm225 Posted July 8, 2007 Author Share Posted July 8, 2007 @cooldude832 - thank you for getting the ball rolling! @JP128 - your example works perfectly for me! Thank you so much! Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292319 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 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 Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292320 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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) Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292321 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 i make my arrays too complicated for most questions here so i tend to try and water em down and then i can't use my methods on them. Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292324 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 What do you think that the hardest part about making a multi-d array? Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292325 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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 Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292327 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 that confused me lol Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292329 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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. Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292332 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 Oh ok. Link to comment https://forums.phpfreaks.com/topic/58907-solved-reverse-order-of-directory-listing-produced-by-code/#findComment-292333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.