在PHP中按创建/修改日期对文件进行排序 [英] Sorting files by creation/modification date in PHP

查看:236
本文介绍了在PHP中按创建/修改日期对文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
文件创建时间

Possible Duplicate:
File creation time

在PHP中,如何检索按创建日期(或任何其他排序机制)排序的文件夹中包含的文件?

In PHP, how to retrieve the files contained into a folder sorted by creation date (or any other sorting mechanism)?

根据文档 readdir( )函数:

文件名在 它们存储的顺序 文件系统.

The filenames are returned in the order in which they are stored by the filesystem.

推荐答案

将其信息保存到数组中,对数组进行排序,然后循环数组

save their information to an array, sort the array and then loop the array

if($h = opendir($dir)) {
  $files = array();
  while(($file = readdir($h) !== FALSE)
    $files[] = stat($file);

  // do the sort
  usort($files, 'your_sorting_function');

  // do something with the files
  foreach($files as $file) {
    echo htmlspecialchars($file);
  }
}

这篇关于在PHP中按创建/修改日期对文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆