下面的代码是从给定目录中获取5个图像文件的函数的一部分.
目前,readdir根据规范按照文件系统存储的顺序返回图像.
我的问题是,如何修改它以获取最新的5张图像?基于last_modified日期或文件名(看起来像0000009-16-5-2009.png,0000012-17-5-2009.png等).
if ( $handle = opendir($absolute_dir) )
{
$i = 0;
$image_array = array();
while ( count($image_array) < 5 && ( ($file = readdir($handle)) !== false) )
{
if ( $file != "." && $file != ".." && $file != ".svn" && $file != 'img' )
{
$image_array[$i]['url'] = $relative_dir . $file;
$image_array[$i]['last_modified'] = date ("F d Y H:i:s", filemtime($absolute_dir . '/' . $file));
}
$i++;
}
closedir($handle);
}
Run Code Online (Sandbox Code Playgroud)
sou*_*rge 13
如果要在PHP中完全执行此操作,则必须找到所有文件及其上次修改时间:
$images = array();
foreach (scandir($folder) as $node) {
$nodePath = $folder . DIRECTORY_SEPARATOR . $node;
if (is_dir($nodePath)) continue;
$images[$nodePath] = filemtime($nodePath);
}
arsort($images);
$newest = array_slice($images, 0, 5);
Run Code Online (Sandbox Code Playgroud)