如何首先显示最新上传的图像? (PHP + CSS) [英] How can I display latest uploaded image first? (PHP+CSS)

查看:100
本文介绍了如何首先显示最新上传的图像? (PHP + CSS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,基本上,我试图显示一个从文件夹中检索照片的图像库.问题是,我希望具有最近上传日期的图像出现在最开始的位置,依此类推,直到最旧的图像出现在底部.

I am new to PHP and basically I am trying to display an image gallery that retrieves photos from a folder. The thing is, I want the image with the most recent upload date to appear at the very beginning, and so on until the oldest one in the bottom.

这就是我的PHP的样子(我想很重要)

This is what my PHP looks like (the important bit I guess)

$files = scandir('uploads/thumbs');
$ignore = array( 'cgi-bin', '.', '..');
foreach ($files as $file) {
    if(!in_array($file, $ignore)) {
        echo '<img src="uploads/thumbs/' . $file . '" />';
         }
}

我想知道是否有PHP的方法,或者在CSS的帮助下,可以以相反的顺序显示它们,从而使最新的方法始终出现在页面顶部.

I would like to know if there's a way by PHP or maybe with a little help of CSS to display them in reverse order, making the newest one always to appear at the top of the page.

非常感谢任何帮助或建议,来自阿根廷的问候!

Any help or suggestion is very appreciated, regards from Argentina!

推荐答案

$files旁边,您可以获取,然后根据获取的时间值对$files数组进行排序. array_multisort :

Next to your $files you can obtain the modification time of each file and then sort the $files array based on the time values acquired. A function which sorts two or more arrays with the value of an array is array_multisort:

$files = scandir($path);
$ignore = array( 'cgi-bin', '.', '..');

# removing ignored files
$files = array_filter($files, function($file) use ($ignore) {return !in_array($file, $ignore);});

# getting the modification time for each file
$times = array_map(function($file) use ($path) {return filemtime("$path/$file");}, $files);

# sort the times array while sorting the files array as well
array_multisort($times, SORT_DESC, SORT_NUMERIC, $files);

foreach ($files as $file) {
    echo '<img src="uploads/thumbs/' . $file . '" />';
}

这篇关于如何首先显示最新上传的图像? (PHP + CSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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