使用PHP在文件夹中查找最旧的文件 [英] Find oldest file in a folder using PHP

查看:88
本文介绍了使用PHP在文件夹中查找最旧的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个PHP脚本,该脚本将在文件夹中找到最旧文件的日期.现在,我要遍历每个文件,并将其修改日期"与以前的文件进行比较,并保留最早的日期.

I need a PHP script that will find the date of the oldest file in a folder. Right now, I am iterating over every file and comparing it's Date Modified to the previous file and keeping the oldest date stored.

是否有较少的磁盘/内存密集型方式来执行此操作?该文件夹中大约有30万个文件.如果我在Windows资源管理器中打开该文件夹,它将按修改的日期自动排序,并且我看到最早的文件要快得多.有什么办法可以利用PHP脚本利用Explorer的默认排序方式?

Is there a less disk/memory intensive way to do this? There are about 300k files in the folder. If I open the folder in Windows Explorer, it will auto sort by date modified and I see the oldest file a lot faster. Is there any way I can take advantage of Explorer's default sort from a PHP script?

推荐答案

// Grab all files from the desired folder
$files = glob( './test/*.*' );

// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_ASC,
$files
);

echo $files[0] // the latest modified file should be the first.

来自网站

祝你好运

要排除目录中的文件以减少不必要的搜索,

To exclude files in the directory to reduce unnecessary searches:

$files = glob( './test/*.*' );
$exclude_files = array('.', '..');
if (!in_array($files, $exclude_files)) {
// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_ASC,
$files
);
}

echo $files[0];

如果您知道要查找的内容和可以排除的内容,这将很有帮助.

This is helpful if you know what to look for and what you can exclude.

这篇关于使用PHP在文件夹中查找最旧的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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