如何先显示最新上传的图片? (PHP+CSS)
我是 PHP 新手,基本上我正在尝试显示一个从文件夹中检索照片的图像库。问题是,我希望具有最新上传日期的图像出现在最开始的位置,依此类推,直到最旧的图像出现在底部。
这就是我的 PHP 的样子(我猜是重要的一点)
$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 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.
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 . '" />';
}
}
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的图像在数据库中,您只需执行
$sql1 = "SELECT * FROM images ORDER BY img_id DESC";
If you have your images in a database you can just do
$sql1 = "SELECT * FROM images ORDER BY img_id DESC";
在您的
$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 isarray_multisort
: