Jump to content

Help me sort files by date?


Moron

Recommended Posts

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

  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);

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 />";
}
  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).

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";
}

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?

  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?

  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.

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.