簡體   English   中英

使用PHP在目錄中列出最近3天的項目

[英]Listing last 3 days items in directory using PHP

我有以下PHP代碼來列出目錄文件夾中的數據,我的問題是如何只列出最近三天的值? 其次,僅詢問,但是是否可以在此代碼中添加分頁?

<?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>\n";
    echo "<td><img src=\"{$file['name']}\" width=\"64\" alt=\"\"></td>\n";
    echo "<td>{$file['name']}</td>\n";
    echo "<td>{$file['type']}</td>\n";
    echo "<td>{$file['size']}</td>\n";
    echo "<td>",date('r', $file['lastmod']),"</td>\n";
    echo "</tr>\n";
  }
?>
</tbody>
</table>

如果您只想列出少於三天前修改過的文件

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

如果要添加分頁,則應將過濾器放在getFileList而不是顯示在屏幕上,以便您可以輕松計算擁有的可顯示項目的數量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM