Moron Posted September 18, 2013 Share Posted September 18, 2013 I'm displaying the contents of a folder, with each file clickable. I can't seem to get it to sort by file date, with the most current on top (descending). What am I missing here? <?php echo "<h1>File Listing</h1>"; $content_array = array(); $path = "../FOLDER/Subfolder/"; $dh = opendir($path); $handle=opendir($dirname); $i=0; while (($file = readdir($dh)) !== false) { if ($file != "." && $file != "..") { $content_array[$i][0] = $file; $content_array[$i][1] = date ("Y m d", filemtime($dirname."/".$file)); $i++; } foreach($content_array as $res) $sortAux[] = $res[1]; array_multisort($sortAux, SORT_DESC, $content_array); echo "<a href='$path/$file' target=\"\_blank>$file</a><br />"; } closedir($dh); ?> Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/ Share on other sites More sharing options...
cataiin Posted September 18, 2013 Share Posted September 18, 2013 $where = "path/"; chdir($where); array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files); var_dump($files); Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450092 Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 On 9/18/2013 at 5:18 PM, cataiin said: $where = "path/"; chdir($where); array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files); var_dump($files); Very nice! One liner: array_multisort(array_map('filemtime', ($files = array_map('basename', $files = glob("path/*.*")))), SORT_DESC, $files); Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450097 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 Sorry to be so dense, but I don't write PHP every day. How do I integrate this into my existing code? Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450103 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 In other words, my code does exactly what I want except for the sorting. Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450105 Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 You had a lot of unneeded code. The one line lists all files and sorts them by date descending. There are different ways to handle the path, but this should work fine: $path = "../FOLDER/Subfolder"; array_multisort(array_map('filemtime', ($files = glob("$path/*.*"))), SORT_DESC, $files); foreach($files as $file) { echo "<a href='$file' target=\"\_blank>".basename($file)."</a><br />"; } Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450107 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 On 9/18/2013 at 6:05 PM, AbraCadaver said: You had a lot of unneeded code. The one line lists all files and sorts them by date descending. There are different ways to handle the path, but this should work fine: $path = "../FOLDER/Subfolder"; array_multisort(array_map('filemtime', ($files = glob("$path/*.*"))), SORT_DESC, $files); foreach($files as $file) { echo "<a href='$file' target=\"\_blank>".basename($file)."</a><br />"; } Thanks. That's closer, but it's sorting by file name (descending) and I need it to sort by file modified date (also descending, newest on top). Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450108 Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 On 9/18/2013 at 6:10 PM, Moron said: Thanks. That's closer, but it's sorting by file name (descending) and I need it to sort by file modified date (also descending, newest on top). Not for me it's not. Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450109 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 On 9/18/2013 at 6:13 PM, AbraCadaver said: Not for me it's not. Ah. You're absolutely right I ot momentarily confused by the file names. Thanks a ton! . Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450110 Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 Just for completeness, because I could see someone coming back and asking, if you want to store/use the dates as well: array_multisort(($times = array_map('filemtime', ($files = glob("$path/*.*")))), SORT_DESC, $files); foreach($files as $i => $file) { echo "<a href='$file' target=\"\_blank>".basename($file)."</a> : ".date("Y-m-d", $times[$i])."<br />\n"; } Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450111 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 Now there's a weird problem. I navigate to the folder in Windows Explorer and click a file, it has tons of information. I click it from this PHP page and it has just a couple of lines. It's obviously a different file, but the names match exactly. This is total Twilight Zone. Ideas? Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450118 Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 On 9/18/2013 at 6:32 PM, Moron said: Now there's a weird problem. I navigate to the folder in Windows Explorer and click a file, it has tons of information. I click it from this PHP page and it has just a couple of lines. It's obviously a different file, but the names match exactly. This is total Twilight Zone. Ideas? What type of file? Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450121 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 Aha! For some crazy reason, it will display one file name, but point to a different one! So where's the bug? Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450122 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 On 9/18/2013 at 6:36 PM, AbraCadaver said: What type of file? They're plain ASCII text files. Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450123 Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 On 9/18/2013 at 6:38 PM, Moron said: They're plain ASCII text files. And when you open in Windows Explorer and from the browser do they open in the same app? Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450129 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 On 9/18/2013 at 6:43 PM, AbraCadaver said: And when you open in Windows Explorer and from the browser do they open in the same app? No, From Windows Explorer, they open in Notepad. From the browser, they open in a browser window. You can hover the mouse over each file listed and look at the bottom bar of the browser, and it will be pointing to a different file name. That's why they aren't matching. Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450131 Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 If that is really the case, then it is probably because you are using the relative ../path/etc. Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450132 Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 On 9/18/2013 at 6:48 PM, AbraCadaver said: If that is really the case, then it is probably because you are using the relative ../path/etc. Yes, I'm using a relative path. If I use the full path (http://server/folder/subfolder, etc.....), it crashes. Link to comment https://forums.phpfreaks.com/topic/282251-help-me-sort-files-by-date/#findComment-1450134 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.