【问题标题】:scandir() to sort by date modifiedscandir() 按修改日期排序
【发布时间】:2012-08-09 00:41:46
【问题描述】:

我正在尝试使scandir(); 函数超出其书面限制,我需要的不仅仅是它目前支持的 alpha 排序。我需要对scandir(); 结果进行排序,以按修改日期排序。

我尝试了一些我在这里找到的解决方案以及来自不同网站的一些其他解决方案,但没有一个对我有用,所以我认为我在这里发帖是合理的。

到目前为止我尝试过的是:

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

但是,这似乎对我不起作用,它只返回 3 个结果,但它应该返回 16 个结果,因为文件夹中有 16 个图像。

【问题讨论】:

    标签: php arrays sorting scandir


    【解决方案1】:
    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;
    }
    

    【讨论】:

    • @dugi 试试这个方法。单个目录中的文件名应该是唯一的。因此,它们可以用作密钥。然后数组按值(修改时间)排序,键(文件名)以正确的顺序返回。
    • 效果很好。要忽略所有点文件,您可以使用类似数组的字符串索引而不是 in_array 来检查文件名的第一个字符: if ($file[0] === '.') continue;
    • 你保存了我的应用。谢谢亲爱的
    【解决方案2】:

    替代示例..

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

    【讨论】:

      【解决方案3】:

      这是一个很好的问题,Ryon Sherman’s answer 提供了一个可靠的答案,但我需要更多的灵活性来满足我的需求,所以我创建了这个新功能:better_scandir

      目标是让scandir 排序顺序标志按预期工作;不仅仅是 Ryon 答案中的反向数组排序方法。并且还为数组排序显式设置SORT_NUMERIC,因为这些时间值显然是数字。

      用法是这样的;只需将SCANDIR_SORT_DESCENDING 切换为SCANDIR_SORT_ASCENDING 甚至将其留空即可:

      better_scandir(<filepath goes here>, SCANDIR_SORT_DESCENDING);
      

      这是函数本身:

      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
      

      【讨论】:

        【解决方案4】:

        另一个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]);
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-12
          • 2016-04-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-03
          • 1970-01-01
          相关资源
          最近更新 更多