按修改日期对图像排序

时间:2016-10-31 12:37:03

标签: php sorting lightbox

我有一个小脚本,可以将文件夹中的图像放入网页中。 我想按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;

1 个答案:

答案 0 :(得分:0)

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

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