【问题标题】:Easy way to sort getting images from a gallery using php?使用 php 对从图库中获取图像进行排序的简单方法?
【发布时间】:2013-01-23 13:49:50
【问题描述】:

你好,stackoverflow 的好人。

我有这段代码,基本上可以获取我放置在名为“图片”的文件夹中的任何照片,并将它们显示在我的页面上。这一切都很好,它也适用于灯箱。

我的主要问题是,有没有一种简单的方法可以以某种顺序显示照片?即第一张最新照片?

<?php $handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){if($file !== '.' && $file !== '..'){
echo '<a href="pictures/'.$file.'" rel="lightbox"><img src="pictures/'.$file.'" border="0" /></a>';}}?>

我知道代码非常粗俗和古老,但它只是一个示例页面,所以它不需要很大。

【问题讨论】:

    标签: php image file readdir opendir


    【解决方案1】:

    您可以使用 php 的 filemtime() 来订购它们:http://php.net/manual/en/function.filemtime.php

    <?php 
       $path = dirname(realpath(__FILE__)).'/pictures/';
       $handle = opendir($path);
       $arrFiles = array();
    
       while($file = readdir($handle))
       {
          if($file !== '.' && $file !== '..')
          {
              $arrFiles[filemtime($path.$file)] = '<a href="pictures/'.$file.'" rel="lightbox"><img src="pictures/'.$file.'" border="0" /></a>';
          }
       }
       arsort($arrFiles);
       foreach ($arrFiles as $file)
       {
           echo $file;
       }
    ?>
    

    【讨论】:

    • 刚刚收到大量错误页面。第 26 行是 { $arrFiles[filemtime($handle.$file)] = ''; }
    • 警告:filemtime(): stat failed for Resource
    • @SeanKerwin 对不起。我的原始代码有问题。我试图将 $handle 附加到 $file。当然那没有用。检查上面的修复。
    • 似乎工作正常!我刚刚上传了 4 张图片,一张一张,我上传的最后一张是显示的第一张图片。荣誉。
    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 2015-03-31
    • 1970-01-01
    • 2011-09-03
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多