donglu1913 2014-09-10 17:19
浏览 17
已采纳

使用PHP在目录中列出最近3天的项目

I have the following PHP code to list data from a directory folder, my question is how do I just list the values for the last three days? Secondly, Just asking but is it possible to add pagination to this code?

<?PHP

  function getFileList($dir)
  {
    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry/",
          "type" => filetype("$dir$entry"),
          "size" => 0,
          "lastmod" => filemtime("$dir$entry")
        );
      } elseif(is_readable("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry",
          "type" => mime_content_type("$dir$entry"),
          "size" => filesize("$dir$entry"),
          "lastmod" => filemtime("$dir$entry")
        );
      }
    }
    $d->close();

    return $retval;
  }
?>

<h1>Display PNG images in a TABLE</h1>

<table class="collapse" border="1">
<thead>
<tr><th></th><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>
</thead>
<tbody>
<?PHP
  $dirlist = getFileList("./images");
  foreach($dirlist as $file) {
    if(!preg_match("/\.png$/", $file['name'])) continue;
    echo "<tr>
";
    echo "<td><img src=\"{$file['name']}\" width=\"64\" alt=\"\"></td>
";
    echo "<td>{$file['name']}</td>
";
    echo "<td>{$file['type']}</td>
";
    echo "<td>{$file['size']}</td>
";
    echo "<td>",date('r', $file['lastmod']),"</td>
";
    echo "</tr>
";
  }
?>
</tbody>
</table>
  • 写回答

1条回答 默认 最新

  • duanmuyao0463 2014-09-10 17:28
    关注

    If you only want to list files that were modified less than 3 days ago

    if(!preg_match("/\.png$/", $file['name'])
      || $file['lastmod'] > strtotime('-3 day')
    ) continue;
    

    If you want to add pagination, you should put the filter in getFileList instead of on display so you can easily count the # of displayable items you have.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题