使用filemtime()按修改时间排序文件


order files by modification time using filemtime()

我有一个脚本,它给了我各种文件的url。我必须使用filemtime将它们与上次修改时间一起订购。脚本:

 <?php  
            while ($row = mysql_fetch_assoc($rs_result)) {  
            $string = $row["imagens"];
            $array=explode(",",$string);

            foreach ($array as $var) {

            ?>  
                <div class="col-md-3 portfolio-item crop">
                    <a href="uploads/<?php echo $var;?>" data-lightbox="proejtos">
                        <img class="img-responsive" src="uploads/<?php echo $var;?>" alt="">
                    </a>
                </div>
              <?php  
            }};  
            ?>    

我想这就是你想要的

  $sorting = array();
  //while whatever images you get
  while($row = mysql_fetch_assoc($rs_result)){
    $url = $row['url'];/*Put your images url*/

    //get the time of the file
    $timestamp = date("m-d-Y H:i:s",filemtime($url));
    //add the time and url of the image
    $sorting[$timestamp] = $url;
  }

  //sort the array with respect to time
  ksort($sorting);
  foreach($sorting as $x => $x_value) {
      echo "Image uploaded on = " . $x . ", has url : " . $x_value;
      echo "<br>";
  }

输出:

图片上传时间:2016年4月15日07:17:54,网址:advertise/150420161846008-QPLVCzm0qi146048950191-3.png

图片上传关于=2016年4月15日18:24:19,有网址:advertise/15042016184608-QPLVCzm0qi146048950191-1.png

图片上传关于=04-17-2016 18:28:38,有网址:advertise/1704201682838-QPLVCzm0qi1460489950191-5.png

按降序设置:
函数ksort()按递增顺序排列邮票,其中为了按递减顺序排列邮票(要将新图像/文件放在上面),使用函数krsort()。ksort/krsort函数按照键的递减或递增顺序排列数组。为了根据值而不是键来排列数组,使用了函数asort()。

表中必须有creted_at(例如)字段。将此字段的值设置为实际时间。然后,当你想按日期(时间)订购商品时,你可以简单地通过查询获得这个值并订购。BTW:我建议你使用一些像Laravel这样的PHP框架,它们让这变得非常容易,你不必关心这些事情,它会帮你做的。