Sorting directory files by creation date in php

Go To StackoverFlow.com

1

Here is the code I use to get an array of the files in a directory.

$dir = "thumbnails/";
$images = scandir($dir);

How can I sort the $images array by creation date? I found a couple of ways but I couldn't get any of them to work with my array.

Thanks,

2012-04-05 01:17
by user541597
Why don't you post what you tried - qitch 2012-04-05 01:22
arraymultisort(arraymap('filemtime', $images), SORT_DESC, $images) - user541597 2012-04-05 01:23


1

On Windows you can get the creation time of the file with filectime(). Just put it in an array with the filename and sort the array.

Creation time is not stored on most *nix filesystems.

2012-04-05 01:23
by Ignacio Vazquez-Abrams
its a ftp server running linux - user541597 2012-04-05 01:24


0

try the following tutorial that includes a script on how to sort by file modification/creation.

http://www.bitrepository.com/sort-files-by-filemtime.html

2012-04-05 01:29
by COLD TOLD


0

You have to sort manually

$dir = "thumbnails/";

function compare_time($a, $b)
{
    global $dir;

    $timeA = filectime("$dir/$a");
    $timeB = filectime("$dir/$b");

    if($timeA == $timeB) return 0;

    return ($timeA < $timeB) ? -1 : 1;
}

$images = scandir($dir);
usort($images, 'compare_time');
2012-04-05 01:32
by Saulo Vallory
On unix system you should use filemtime instead of filectime - Saulo Vallory 2012-04-05 01:35
how would I access each of the items in this array - user541597 2012-04-05 01:48
it's simply an int indexed array of file names. something like

Array([0] => '.', [1] => '..', [2] => 'afile.jpg');

have a look at php.net/scandirSaulo Vallory 2012-04-10 01:16

Ads