0

I was getting my photo from a folder randomly as the code below:

$imagesDir = 'tags/chilli_crab/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];

How can I change this to get the latest photo instead of the random one? Thanks!!!!!!

4

2 回答 2

2
array_multisort(array_map('filemtime', $images), SORT_NUMERIC, SORT_DESC, $images);

$latestimage = $images[0];
于 2013-09-09T04:39:49.183 回答
0

使用以下代码:

$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);

希望这个对你有帮助

于 2013-09-09T04:36:31.387 回答