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条)

报告相同问题?