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); ?> Quote Link to comment Share on other sites More sharing options...
cataiin Posted September 18, 2013 Share Posted September 18, 2013 (edited) $where = "path/"; chdir($where); array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files); var_dump($files); Edited September 18, 2013 by cataiin Quote Link to comment Share on other sites More sharing options...
AbraCadaver 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); Very nice! One liner: array_multisort(array_map('filemtime', ($files = array_map('basename', $files = glob("path/*.*")))), SORT_DESC, $files); Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Solution AbraCadaver Posted September 18, 2013 Solution 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 />"; } Quote Link to comment Share on other sites More sharing options...
Moron Posted September 18, 2013 Author 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 />"; } 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). Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 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. Quote Link to comment Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 Not for me it's not. Ah. You're absolutely right I ot momentarily confused by the file names. Thanks a ton! . Quote Link to comment 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"; } Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 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? What type of file? Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 What type of file? They're plain ASCII text files. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 18, 2013 Share Posted September 18, 2013 They're plain ASCII text files. And when you open in Windows Explorer and from the browser do they open in the same app? Quote Link to comment Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Moron Posted September 18, 2013 Author Share Posted September 18, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.