Scandir() to Sort by Date Modified

scandir() to sort by date modified

function scan_dir($dir) {
$ignored = array('.', '..', '.svn', '.htaccess');

$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, $ignored)) continue;
$files[$file] = filemtime($dir . '/' . $file);
}

arsort($files);
$files = array_keys($files);

return ($files) ? $files : false;
}

filter scandir and sort by date created

You can use glob() to get an array of filenames then use usort() and filectime() to order them by their creation dates:

$files = glob('*.markdown');
usort($files, function($file_1, $file_2)
{
$file_1 = filectime($file_1);
$file_2 = filectime($file_2);
if($file_1 == $file_2)
{
return 0;
}
return $file_1 < $file_2 ? 1 : -1;
});

For PHP versions lower than 5.3:

function sortByCreationTime($file_1, $file_2)
{
$file_1 = filectime($file_1);
$file_2 = filectime($file_2);
if($file_1 == $file_2)
{
return 0;
}
return $file_1 < $file_2 ? 1 : -1;
}

$files = glob('*.markdown');
usort($files, 'sortByCreationTime');

Getting file names sorted by time using scandir

Since hashing function like md5 are one-way only, the filename is useless as a sorting criteria. If you want to keep track of the very same timestamp you used for generating the md5 value, you'd have to keep a hash:timestamp table on record. If you did that, you wouldn't need to run scandir to begin with -- you could simply read the file list from the reference table you've saved. (Assuming you keep it up to date with deleted files. Otherwise, it would show obsolete files.)

Is there a particular reason you need to use a md5-hash of the timestamp? Why not simply use the timestamp (with a prefix or otherwise) as the filename? Then you could simply sort alphabetically, ascending or descending, and have the files automatically in timewise order. This would be by far the simplest and most light-weight option.

If md5-hashes as file names is a must, and writing a reference table is not what you prefer, then you will have to loop through the files, or use usort, and check the date of the file's creation/modification (filemtime). You can find solutions in the answers to sort files by date in PHP. Be aware that this will lead to plenty more disk activity (even if the results are cached).

sorting scandir by date Descending order

$files = glob('back/1/*',GLOB_ONLYDIR);
foreach ($files as $f){
$tmp[basename($f)] = filemtime($f);
}
asort($tmp);
$files = array_keys($tmp);

How to sort files by date in PHP

You need to put the files into an array in order to sort and find the last modified file.

$files = array();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[filemtime($file)] = $file;
}
}
closedir($handle);

// sort
ksort($files);
// find the last modification
$reallyLastModified = end($files);

foreach($files as $file) {
$lastModified = date('F d Y, H:i:s',filemtime($file));
if(strlen($file)-strpos($file,".swf")== 4){
if ($file == $reallyLastModified) {
// do stuff for the real last modified file
}
echo "<tr><td><input type=\"checkbox\" name=\"box[]\"></td><td><a href=\"$file\" target=\"_blank\">$file</a></td><td>$lastModified</td></tr>";
}
}
}

Not tested, but that's how to do it.



Related Topics



Leave a reply



Submit