duanchuanqu593743 2019-02-28 19:48
浏览 57

按文件创建顺序/修改日期不工作PHP

Right, this is really frustrating me.

I have 3 directions (dir1/dir2/dir3) when an index.php file in the root which I have posted below.

Each folder has a list of images in, they are automatically named and place into each of the folders. These are displayed in a table in 3 columns, with another 2 columns before these to show the number of rows and the date and time of each file. The first column of the 3 directories is suppose to order by DESC to show the latest file first. However, it shows the date and time in the wrong order, for example at the top it says February 28th (time) then another 28th of February (time) one, then two 26th of February (time), then some more of 28th!? Where it should be sorting latest dates first then oldest below. Can any one assist?

<?php
date_default_timezone_set('Europe/London');
$dirname = "dir1";
$dirnameTwo = "dir2";
$dirnameThree = "dir3";

$cam1 = scandir($dirname, SORT_DESC);
$cam2 = scandir($dirnameTwo, SORT_DESC);
$cam3 = scandir($dirnameThree, SORT_DESC);
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html>
<head>
    <meta http-equiv='refresh' content='10'>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <meta http-equiv='cache-control' content='max-age=0' />
    <meta http-equiv='cache-control' content='no-cache' />
    <meta http-equiv='expires' content='0' />
    <meta http-equiv='expires' content='Tue, 01 Jan 1980 1:00:00 GMT' />
    <meta http-equiv='pragma' content='no-cache' />
</head>

<body>
<style type="text/css">
    .pi-title {
        padding: 1rem;
    }
</style>
<div class="container">
    <div class="row">
        <div class="pi-title">
            <h3>Test</h3>
        </div>
        <div class="table-container col-md-12">
            <table class="table" border='1' cellpadding='5' cellspacing='0' bordercolor='#ccc'>
                <thead class="thead-dark">
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">File Name</th>
                    <th scope="col">1 </th>
                    <th scope="col">2</th>
                    <th scope="col">3</th>
                </tr>
                </thead>
                <tbody>
                <tr></tr>
                <tr>
                    <?php
                    error_reporting(E_ALL & ~E_NOTICE);
                    $directories = implode(',', [ $dirname, $dirnameTwo, $dirnameThree]);
                    $items = glob("./{{$directories}}/*", GLOB_BRACE);
                    $dirs = array($dirname, $dirnameTwo, $dirnameThree);
                    for ($i = 1; $i <= 12; $i++) {
                        if (file_exists($items[$i])) {
                            echo "</tr>";
                            echo "<td><font face='Arial' size='3'>$i</font></td>";
                            echo "<td><font face='Arial' size='3' color='red'>" . date("F d Y H:i", filemtime($items[$i]));
                            echo "</font></td>";
                        }
                        print("

           <td><img src='$dirs[0]/$cam1[$i]' height='80' width='80'></td>
           <td><img src='$dirs[1]/$cam2[$i]' height='80' width='80'></td>
           <td><img src='$dirs[2]/$cam3[$i]' height='80' width='80'></td>

   ");
                        if ($i === 12) break;
                    }
                    ?>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dongtuo7364 2019-02-28 20:26
    关注

    scandir orders ascending or descending alphabetically. But you want to sort by the date it has been last modified. So you need to apply a sorting function on it. For example you can do it with filemtime and uasort

    $items = glob ...
    uasort($items, function($a,$b) { return filemtime($a) > filemtime($b); })
    

    Flip filemtime($a) > filemtime($b) to filemtime($a) < filemtime($b) for asc/desc http://php.net/manual/en/function.scandir.php#refsect1-function.scandir-parameters

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大