简体   繁体   English

scandir() 按修改日期排序

[英]scandir() to sort by date modified

I'm trying to make scandir();我正在尝试制作scandir(); function go beyond its written limits, I need more than the alpha sorting it currently supports.函数超出了它的书面限制,我需要的不仅仅是它目前支持的 alpha 排序。 I need to sort the scandir();我需要对scandir(); results to be sorted by modification date.结果按修改日期排序。

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 个图像。

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;
}

This is a great question and Ryon Sherman's answer provides 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 scandir sorting order flags work as expected;目标是让scandir排序标志按预期工作; not just the reverse array sort method in Ryon's answer.不仅仅是 Ryon 答案中的反向数组排序方法。 And also explicitly setting SORT_NUMERIC for the array sort since those time values are clearly numbers.并且还为数组排序显式设置SORT_NUMERIC ,因为这些时间值显然是数字。

Usage is like this;用法是这样的; just switch out SCANDIR_SORT_DESCENDING to SCANDIR_SORT_ASCENDING or 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

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>"; 
}  

Another scandir keep latest 5 files:另一个scandir保留最新的 5 个文件:

public function checkmaxfiles()
{

    $dir = APPLICATION_PATH . '\\modules\\yourmodulename\\public\\backup\\';
    // '../notes/';
    $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);
    $length = count($files);
    if($length < 4 ){
        return;
    }
    for ($i = $length; $i > 4; $i--) {
           echo "Erase : " .$dir.$files[$i];
           unlink($dir.$files[$i]);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM