php scandir() 按修改日期排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11923235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scandir() to sort by date modified
提问by aborted
I'm trying to make scandir();function go beyond its written limits, I need more than the alpha sorting it currently supports. I need to sort the scandir();results to be sorted by modification date.
我试图让scandir();函数超出其书面限制,我需要的不仅仅是它目前支持的 alpha 排序。我需要对要按scandir();修改日期排序的结果进行排序。
I've tried a few solutions I found here and some other solutions from different websites, but none worked for me, so I think it's reasonable for me to post here.
我已经尝试了一些我在这里找到的解决方案以及来自不同网站的其他一些解决方案,但没有一个对我有用,所以我认为我在这里发帖是合理的。
What I've tried so far is this:
到目前为止我尝试过的是:
function scan_dir($dir)
{
$files_array = scandir($dir);
$img_array = array();
$img_dsort = array();
$final_array = array();
foreach($files_array as $file)
{
if(($file != ".") && ($file != "..") && ($file != ".svn") && ($file != ".htaccess"))
{
$img_array[] = $file;
$img_dsort[] = filemtime($dir . '/' . $file);
}
}
$merge_arrays = array_combine($img_dsort, $img_array);
krsort($merge_arrays);
foreach($merge_arrays as $key => $value)
{
$final_array[] = $value;
}
return (is_array($final_array)) ? $final_array : false;
}
But, this doesn't seem to work for me, it returns 3 results only, but it should return 16 results, because there are 16 images in the folder.
但是,这似乎对我不起作用,它只返回 3 个结果,但它应该返回 16 个结果,因为文件夹中有 16 个图像。
回答by Ryon Sherman
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;
}
回答by JakeGould
This is a great question and Ryon Sherman's answerprovides a solid answer, but I needed a bit more flexibility for my needs so I created this newer function: better_scandir.
这是一个很好的问题,Ryon Sherman 的回答提供了可靠的答案,但我需要更多的灵活性来满足我的需求,所以我创建了这个更新的函数:better_scandir.
The goal is to allow having scandirsorting order flags work as expected; not just the reverse array sort method in Ryon's answer. And also explicitly setting SORT_NUMERICfor the array sort since those time values are clearly numbers.
目标是允许scandir排序顺序标志按预期工作;不仅仅是 Ryon 的答案中的反向数组排序方法。并且还明确设置SORT_NUMERIC了数组排序,因为这些时间值显然是数字。
Usage is like this; just switch out SCANDIR_SORT_DESCENDINGto SCANDIR_SORT_ASCENDINGor even leave it empty for default:
用法是这样的;只需切换SCANDIR_SORT_DESCENDING到SCANDIR_SORT_ASCENDING或什至将其留空为默认值:
better_scandir(<filepath goes here>, SCANDIR_SORT_DESCENDING);
And here is the function itself:
这是函数本身:
function better_scandir($dir, $sorting_order = SCANDIR_SORT_ASCENDING) {
/****************************************************************************/
// Roll through the scandir values.
$files = array();
foreach (scandir($dir, $sorting_order) as $file) {
if ($file[0] === '.') {
continue;
}
$files[$file] = filemtime($dir . '/' . $file);
} // foreach
/****************************************************************************/
// Sort the files array.
if ($sorting_order == SCANDIR_SORT_ASCENDING) {
asort($files, SORT_NUMERIC);
}
else {
arsort($files, SORT_NUMERIC);
}
/****************************************************************************/
// Set the final return value.
$ret = array_keys($files);
/****************************************************************************/
// Return the final value.
return ($ret) ? $ret : false;
} // better_scandir
回答by NovaYear
Alternative example..
替代示例..
$dir = "/home/novayear/public_html/backups";
chdir($dir);
array_multisort(array_map('filemtime', ($files = glob("*.{sql,php,7z}", GLOB_BRACE))), SORT_DESC, $files);
foreach($files as $filename)
{
echo "<a>".substr($filename, 0, -4)."</a><br>";
}

