dongyan3853 2017-08-15 18:06
浏览 21
已采纳

需要从文件夹中排序和打印图像[关闭]

I've a problem I'm trying to create a website where you can upload a pic and after the upload you see the pic that the previous user uploaded. The images are stored in a folder in my domain but I don't know how to sort them by date, actually I use this code but it didn't go very well

<?php
$search_dir = "uploads/".$row['Reference_No'];
$images = glob("$search_dir/*.jpg");
sort($images);
//display first image
if (count($images) > 0) { // at least one image exists
    $img = $images[0];// first image
    echo "<img src='../../$img'  border='0' /> ";
} else {
    shuffle($images);
    echo "<img src='../../$images'  border='0' /> ";        
}
  • 写回答

2条回答 默认 最新

  • duanping6698 2017-08-16 05:10
    关注

    Extract the .jpg's from the target directory, store the last modified timestamp and filename, sort by timestamp then filename (with path removed), sort, then loop again to output the array.

    $array=[];
    foreach(glob("uploads/{$row['Reference_No']}/*.jpg") as $filename){
        // store modified timestamp and path-free filename
        $array[]=[filemtime($filename),substr($filename,strrpos($filename,'/')+1)];
    }
    sort($array);  // sort on modified timestamp, then on filename to break any ties
    foreach($array as $a){
        echo $a[1],'<br>';  // display the filename
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库