按修改日期排序图像


Sort Images by Date Modified

我有一个小脚本,把图片从一个文件夹到一个网页。我想按日期修改排序有人知道怎么做吗?

function php_thumbnails($imagefolder,$thumbfolder,$lightbox)
{
//Get image and thumbnail folder from function
$images = "portfolio/" . $imagefolder; //The folder that contains your images. This folder must contain ONLY ".jpg files"!
$thumbnails = "portfolio/" . $thumbfolder; // the folder that contains all created thumbnails.
//Load Images
//load images into an array and sort them alphabeticall:
$files = array();
if ($handle = opendir($images))
    {
    while (false !== ($file = readdir($handle)))
        {
        //Only do JPG's
        if(eregi("((.jpeg|.jpg)$)", $file))
            {
            $files[] = array("name" => $file);
            }
        }
    closedir($handle);
    }
//Obtain a list of columns
foreach ($files as $key => $row)
    {
    $name[$key]  = $row['name'];
    }
//Put images in order:
array_multisort($name, SORT_ASC, $files);
//set the GET variable name
$pic = $imagefolder;

您需要使用filemtime函数来检索文件修改时间,然后使用它来构建您的multisort 帮助数组。

...
if(eregi("((.jpeg|.jpg)$)", $file))
    {
    $datem = filemtime($images . '/' . $file);
    $files[] = array("name" => $file, "date" => $datem);
    }
}
...
...
...
foreach ($files as $key => $row)
{
   $date[$key]  = $row['date'];
}
//Put images in order:
array_multisort($date, SORT_ASC, $files);