Jump to content

Help me sort files by date?


Go to solution Solved by AbraCadaver,

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

$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);
  • Solution

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

 

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?

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.