views:

148

answers:

4

How do I sort a directory by file creation, latest first in PHP 5.2?

I am on Windows 2000.

+1  A: 

Have a look at this tip.

Darmen
+3  A: 

File creation time does not exist on most operating systems.

Ignacio Vazquez-Abrams
Not with windows.
ghostdog74
+1  A: 

You can't for creation date on Unix system, but for change date, but see edit below for Windows.

Code is as follows:

function sortByChangeTime($file1, $file2)
{
    return (filectime($file1) < filectime($file2)); 
}
$files = glob('*');                // get all files in folder
usort($files, 'sortByChangeTime'); // sort by callback
var_dump($files);                  // dump sorted file list

From the Notes on the PHP Manual on filectime:

Note: Note also that in some Unix texts the ctime of a file is referred to as being the creation time of the file. This is wrong. There is no creation time for Unix files in most Unix filesystems.

See the link in Antti's answers for the differences in ctime, atime and mtime and decide for your Usecase which is the most suited when you are on Unix.

Edit: one of the comments at the page for filectime notes that there is no change time on Windows and filectime indeed returns the creation time. I'm no pro on Windows Filesystems, but using the above is then probably your best bet. Alternatively, you can look into the COM API to get a handle on the ScriptingHost and see if you can get the Creation Time from there.

Gordon
Except in windows servers; that's not creation time, that's changed time
Matt Ellen
And the OP asked for Windows, right?
Gordon
I think his comment came before the OP added the windows bit.
hobodave
Btw, why the usort()? I've always found it to be almost an order of magnitude slower than the native array sort methods.
hobodave
@hobodave which other function would allow you to provide a callback to sort the values? And it's not like it is so slow that it will create a bottleneck, right? We're probably talking micro or nanoseconds here.
Gordon
Sorry, I didn't see the windows bit.
Matt Ellen
Yea, there is nothing else. I just like to avoid the u methods like the plague. I've just had to build things where functions could be called 20 - 40k times per page. Trying to optimize them or avoid them altogether then becomes paramount. :)
hobodave
+2  A: 

On windows the CTime is the creation time, use the following:

<?php
$files = array();

foreach (new DirectoryIterator('/path') as $fileInfo) {    
    $files[$fileInfo->getFileName()] = $fileInfo->getCTime();
}

arsort($files);

After this $files will contain an array of your filenames as the keys and the ctime as the values. I chose this backwards representation due to the possibility of identical ctimes, which would cause an entry in $files to be overwritten.

Edit:

Updated answer to use CTime as the OP clarified he's using Windows.

hobodave
This is modification Time not creation Time
Right. Did you read my first line? Most filesystems don't track creation time.
hobodave
no, this won't work. rsort will *lose* the file names.
jspcal
I fixed the typo.
hobodave
@stcony0: I updated my answer to work with windows. Please note that it will not work with *nix.
hobodave
+1 for using Spl - though iterators aren't known to be this fast either ;)
Gordon
Yea, but they got some nice boosts in 5.3.
hobodave