You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于用户输入日期范围展示文件夹内PDF文件的PHP需求

I've adjusted your code to support date range filtering, combine the search forms into one, and add the "Nothing Found" message when no files match. Here's what was changed and the updated code:

Key Improvements

  • Combined both search and date inputs into a single form for better user experience (users can use either or both filters)
  • Added PDF file filtering to ensure only PDF documents are displayed
  • Fixed HTML entity issues in the PHP output (so tags like <pre> work correctly)
  • Properly handled date range comparisons (including the entire day for start/end dates)
  • Added a "Nothing Found" message when no files match the criteria
  • Improved code readability with separate checks for keyword and date matches

Updated main.html Form

<form id="tfnewsearch" method="get" action="search.php">
  <label>Search for Invoice: </label>
  <input type="text" id="tfq2b" class="tftextinput2" name="q" size="25" maxlength="120" placeholder="JobNumber-InvoiceNumber">
  <br><br>
  <label>Date From: </label>
  <input type="date" id="tfq3b" class="tftextinput3" name="dfr" size="14">
  <label style="margin-left:10px;">and Date to: </label>
  <input type="date" id="tfq4b" class="tftextinput4" name="dto" size="14">
  <input type="submit" value="Search" class="tfbutton" style="margin-left:10px;">
</form>

Updated search.php Code

<?php
$dir = './'; 
$exclude = array('.', '..', '.htaccess'); 
$q = isset($_GET['q']) ? strtolower(trim($_GET['q'])) : ''; 

// Process date range inputs
$dateFromTimestamp = null;
$dateToTimestamp = null;

if (!empty($_GET['dfr'])) {
    $dateFrom = DateTime::createFromFormat('Y-m-d', $_GET['dfr']);
    if ($dateFrom) {
        $dateFrom->setTime(0, 0, 0); // Set to start of the selected day
        $dateFromTimestamp = $dateFrom->getTimestamp();
    }
}

if (!empty($_GET['dto'])) {
    $dateTo = DateTime::createFromFormat('Y-m-d', $_GET['dto']);
    if ($dateTo) {
        $dateTo->setTime(23, 59, 59); // Set to end of the selected day
        $dateToTimestamp = $dateTo->getTimestamp();
    }
}

$res = opendir($dir); 
$foundFiles = false;

echo '<pre>'; 
echo '<span style="font-weight:bold;font-size:1.5em;line-height:40px;">Invoice Number</span> 
      <span style="margin-left:80px;font-size:1.5em;font-weight:bold;">Date</span> 
      <span style="margin-left:105px;font-size:1.5em;font-weight:bold;">Click to View</span> 
      <span style="margin-left:60px;font-size:1.5em;font-weight:bold;">Download PDF</span><br>'; 

while(false !== ($file = readdir($res))) { 
    // Skip excluded files and non-PDF documents
    if (in_array($file, $exclude) || pathinfo($file, PATHINFO_EXTENSION) !== 'pdf') {
        continue;
    }
    
    $fileTimestamp = filectime($file);
    $fileDate = date("Y-m-d", $fileTimestamp);
    
    // Check if keyword matches (if provided)
    $keywordMatch = empty($q) || strpos(strtolower($file), $q) !== false;
    
    // Check if date falls within range (if dates are provided)
    $dateMatch = true;
    if ($dateFromTimestamp !== null) {
        $dateMatch &= ($fileTimestamp >= $dateFromTimestamp);
    }
    if ($dateToTimestamp !== null) {
        $dateMatch &= ($fileTimestamp <= $dateToTimestamp);
    }
    
    if ($keywordMatch && $dateMatch) { 
        $foundFiles = true;
        echo "<span style='line-height:20px;font-size:15px;'>$file</span> 
              <span style='margin-left:30px;font-size:15px;'>$fileDate</span> 
              <span style='margin-left:90px;font-size:15px;'><a href='$dir$file'>View</a></span> 
              <span style='margin-left:130px;font-size:15px;'><a href='pdf_server.php?file=$file'>Download</a></span><br>"; 
    } 
} 

// Display message if no files matched
if (!$foundFiles) {
    echo "<span style='font-size:1.2em;color:red;margin-top:20px;display:block;'>Nothing Found</span>";
}

echo '</pre>'; 
closedir($res); 
?>

Notes

  • File Time Consideration: The code uses filectime() which returns the file's change time (when metadata was last modified). If you want to use the file's modification time (when content was last changed), replace filectime($file) with filemtime($file).
  • Styling: I added minor styling tweaks to the form for better spacing, but you can adjust the CSS classes/styles to match your existing design.
  • Validation: Basic date validation is included, but you might want to add error messages if the user enters invalid dates (optional).

内容的提问来源于stack exchange,提问作者KiD Cajes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:19:39