0

I have a chunk of code which scans through a folder an all its sub directories and brings out a json tree as a result.

i am trying to arrange these results as per the date modified so that recent files are shown first

The output JSON array looks like this:

{
    "name": "files",
    "type": "folder",
    "path": "files",
    "date_modified": "September 29 2019 08:37:06",
    "size": 4096,
    "items": [
        {
            "name": "Root Folder",
            "type": "folder",
            "path": "files/data",
            "date_modified": "September 24 2019 08:36:38",
            "size": 64,
            "items": [
                {
                    "name": "Folder 1",
                    "type": "folder",
                    "path": "files/data/Folder 1",
                    "date_modified": "August 15 2019 07:00:33",
                    "size": 64,
                    "items": [
                        {
                            "name": "Data 1.pdf",
                            "type": "file",
                            "path": "files/data/Folder 1/Data 1.pdf",
                            "date_modified": "August 15 2019 07:00:33",
                            "size": 2047292
                        }
                    ]
                },
                {
                    "name": "Folder 2",
                    "type": "folder",
                    "path": "files/data/Folder 2",
                    "date_modified": "September 24 2019 08:37:53",
                    "size": 64,
                    "items": [
                        {
                            "name": "Data 1.pdf",
                            "type": "file",
                            "path": "files/data/Folder 2/Data 2.pdf",
                            "date_modified": "September 24 2019 08:37:53",
                            "size": 1523994
                        }
                    ]
                }
            ]
        }
    ]
}

My php file looks like this:

<?php

$dir = "files";

// Run the recursive function
if(isset($_GET[dir])){
    $dir = $_GET[dir];
}

$response = scan($dir);

function scan($dir){

    $files = array();

    // Is there actually such a folder/file?

    if(file_exists($dir)){

        foreach(scandir($dir) as $f) {

            if(!$f || $f[0] == '.') {
                continue; // Ignore hidden files
            }

            if(is_dir($dir . '/' . $f)) {

                // The path is a folder


                $files[] = array(
                    "name" => $f,
                    "type" => "folder",
                    "path" => $dir . '/' . $f,
                    "date_modified" => date("F d Y H:i:s",filemtime($dir . '/' . $f)),
                    "size" => filesize($dir . '/' . $f),
                    "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder
                );

            }

            else {


                $files[] = array(
                    "name" => $f,
                    "type" => "file",
                    "path" => $dir . '/' . $f,
                    "date_modified" => date("F d Y H:i:s",filemtime($dir . '/' . $f)),
                    "size" => filesize($dir . '/' . $f) // Gets the size of this file
                );

            }
        }

    }

    return $files;
}



// Output the directory listing as JSON

header('Content-type: application/json');

echo json_encode(array(
    "name" => "files",
    "type" => "folder",
    "path" => $dir,
        "date_modified" => date("F d Y H:i:s",filemtime($dir)),
                    "size" => filesize($dir) ,
    "items" => $response
));

?>

To sort my array i have tried the following using

usort

 function date_compare($a, $b)
       {
           $t1 = strtotime($a['datetime']);
           $t2 = strtotime($b['datetime']);
           return $t1 - $t2;
       }
 usort($files, 'date_compare');

but this had the following as an output:

{
    "name": "files",
    "type": "folder",
    "path": "files",
    "date_modified": "September 29 2019 08:37:06",
    "size": 4096,
    "items": true
}

Failing to bring out the sub directories, only bring out 'true'

I have tried several different uses of the usort function including:

usort($files['items']['items'], function($a, $b) {
      return (strtotime($a['date_modified']) < strtotime($b['date_modified']) -1 : 1);
    }); 

And

usort($files, "sortFunction");
    $files = usort($array, function($a1, $a2) {
       $v1 = strtotime(date_format($a1['date_modified'],"F d Y H:i:s"));
       $v2 = strtotime(date_format($a2['date_modified'],"F d Y H:i:s"));
       return $v1 - $v2; 
    });

All failing to arrange this array in the order i require: for every folder i want the latest file (ordered by date modified) to show first.

There are several questions like this, forgive me i couldn't find one with my use case.

Thanks in advance

3
  • 4
    Possible duplicate of Order multidimensional array recursively at each level in PHP Commented Oct 1, 2019 at 8:11
  • You don’t want to sort one single array here - you want to sort multiple arrays on multiple levels. The mentioned duplicate should be able to give you some inspiration. (Note that there ksort is used because they apparently wanted to sort by keys, not values.) Commented Oct 1, 2019 at 8:11
  • Perhaps using this answer in place of scandir($dir) would mean the list of files is sorted by date before adding the full details. Commented Oct 1, 2019 at 8:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.