-1

I'm trying to sort the content of a folder by their date/time. my code bellow gets all the content properly but they are all over the place and they are not sorted by date/time.

my code:

if(isset($_GET['dir'])) {
    $directory = $_GET['dir'];

    $dir = "".$directory."";
    $files = scandir($dir);
    //rsort($files);

    $TIMEANDATE = date("F d Y h:i A");

    foreach ($files as $file) {
        $date = filemtime(''.$dir.'/'.$file.'');
        if ($file != '.' && $file != '..') {
            array_multisort(array_map('filemtime', $file), SORT_ASC, $file); 
            $filesize = human_filesize(filesize(''.$dir.'/'.$file.''));
            $all_doc .= "<tr>
                            <td><input  type='checkbox'/></td>
                            <td>".$fileExt."&nbsp;<a class='defaults'  href='" . $dir ."/". $file . "'>". $file ."</a>&nbsp;&nbsp;<a href=''><i title='Move' class='fa fa-refresh'></i></a>&nbsp;&nbsp;
                                <a href='folder.php?del=" . $dir ."/". $file . "'><i title='Delete' class='fa fa-times'></i></a>
                            </td>           
                            <td>Status</td>
                            <td>".date('F d Y h:i A', $date)."</td>
                            <td>".$filesize."</td>
                            </td>
                        </tr>";
           $i++;
        }

    }
}

I tried this line of code but it doesn't sort them:

array_multisort(array_map('filemtime', $file), SORT_ASC, $file); 

could someone please advise on this?

any help would be appreciated.

Edit: this question has no answer in other posts because the code is clearly different and I cannot change my code to reflect other people's codes on other posts. so I need to work with my own code.

TERO
  • 159
  • 2
  • 16

2 Answers2

1

This should work for you:

(You have a few things in your code which you don't show us what they do, so I modified it so that this code works. Here I just get all files with glob() then I sort them with usort() by the last modification which I get with filemtime() and after this I simply print the files)

<?php

    if(isset($_GET['dir'])) {
        $dir = $_GET['dir'];
        $files = glob($dir . "/*.*");

        usort($files, function($a, $b){
            if(filemtime($a) == filemtime($b))
                return 0;
            return filemtime($a) > filemtime($b) ? 1 : -1;
        });

        echo "<table border='1'>";

        foreach($files as $file) {
        ?>  
        <tr>
            <td><input type="checkbox" /></td>
            <td><?php echo pathinfo($file)["extension"]; ?><a class="defaults"  href="<?php echo $file;?>"><?php echo basename($file);?></a><i title="Move" class="fa fa-refresh"></i><a href="folder.php?del=<?php echo $file; ?>"><i title="Delete" class="fa fa-times"></i></a></td>           
            <td>Status</td>
            <td><?php echo date("F d Y h:i A", filemtime($file));?></td>
            <td><?php echo filesize($file);?></td>  
        </tr>
        <?php
        }

        echo "</table>";

    }

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/73806/discussion-on-answer-by-rizier123-sort-folder-content-by-date-time-using-php). – Taryn Mar 25 '15 at 18:46
  • @bluefeet It wasn't really a discussion, it was just to get the code working for OP. But the code still doesn't work for OP – Rizier123 Mar 25 '15 at 18:48
  • 30 comments was getting excessive so I moved it to chat. Feel free to continue there. – Taryn Mar 25 '15 at 18:49
  • @bluefeet So should I move to chat no matter what the comments are about just if they are like over 20 or so I should use the "move to chat" link ? (for the future) – Rizier123 Mar 25 '15 at 18:51
  • If the discussion starts getting excessive, then yes move it to chat. – Taryn Mar 25 '15 at 18:52
-1
$files = glob('/bla/bla/*');
if (!$files) {
    $files = array();
}

foreach ($files as & $file) {
    $file = array(
        'modified_by' => filemtime($file),
        'file_name'   => $file
    );
}
array_multisort($files);
var_dump($files);
Deep
  • 2,472
  • 2
  • 15
  • 25