【问题标题】:how to show last 10 uploaded images from a folder with php如何使用php显示文件夹中最后10张上传的图像
【发布时间】:2016-01-19 13:13:55
【问题描述】:

我是 php 新手。我需要列出一个文件夹中最后上传的 10 张图片。 现在我有了这个代码。

<?php
$imageDir = "uploads/";
$images = glob($imageDir.'*.jpg');
$flag=1;
foreach ($images as $image){
  echo '<div class="item' .($flag?' active image-resposive':''). '">'.PHP_EOL."\t\t";
?>
<img class="wow zoomIn image-resposive" src="<?php echo $image ?>" alt=""></div> 
<?php 
  $flag=0;
}
?>

【问题讨论】:

  • 您在保存文件的地方运行着任何数据库吗?那可能会容易得多
  • 没有数据库文件保存在名为“upload”的文件夹中

标签: php html image list directory


【解决方案1】:

使用opendir() 循环遍历目录并将所有图像文件名与最后修改日期存储在一个数组中。对该数组进行排序并挑选 10 张图像,以便获得 10 张最近的图像。

【讨论】:

    【解决方案2】:

    这个怎么样。

    $imageDir = "Uploads/";
    $images = glob($imageDir . '\*.jpg');
    
    $latest = array();
    
    foreach($images as $image) {
    
        $x = (string)filectime($image);
        // Incase you encounter duplicates
        // $x = (string)filectime($image) . $image;
    
        $latest[$x] = $image;
    }
    
    krsort($latest);
    $latest = array_slice(array_values($latest), 0, 10);
    
    var_dump($latest);
    

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2023-04-01
      • 2019-12-31
      • 1970-01-01
      相关资源
      最近更新 更多