douren4075 2018-01-04 10:35
浏览 98
已采纳

PHP根据用户输入日期范围在文件夹中显示文件

I have a form that can search pdf files in a folder. I would want to modify my approach wherein there will be a user input of the date range from and to. Then display the pdf files based on the date range if nothing exists then will just simply display "Nothing Found"

I'm not really a PHP expert and I just google to come up with my current code.

                $dir = './'; 
            $exclude = array('.','..','.htaccess'); 
            $q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
            //$date_from = date_create_from_format('Y-m-d',$_GET['dfr']); 
            //$date_to = date_create_from_format('Y-m-d',$_GET['dto']);
            $res = opendir($dir); 
            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))) { 
                if(strpos(strtolower($file),$q)!== false && !in_array($file,$exclude)) {                
                $fileDate = date("Y-m-d", filectime($file));                
                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> "; 
                echo "<br>"; 
                }               
            } 

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

And this is the form code in my main.html

<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" value="JobNumber-InvoiceNumber"><input type="submit" value="&gt;" class="tfbutton2">
        </form>
        <form id="tfnewsearch" method="get" action="search.php">
                <!-- DATE -->
                <label>Date From: </label><input type="date" id="tfq3b" class="tftextinput3" name="dfr" size="14" maxlength="120" value="Date From">
                <label> and Date to: </label>
                <input type="date" id="tfq4b" class="tftextinput4" name="dto" size="14" maxlength="120" value="Date To"><input type="submit" value="&gt;" class="tfbutton4">
        </form>         
  • 写回答

2条回答 默认 最新

  • duanjie3267 2018-01-04 11:39
    关注

    This code should do the job for you:

    EDIT: included the search-parameter q and offset date_to by a day
    EDIT 2: separate search of date and string

    $dir = './';
    $exclude = array('.','..','.htaccess');
    $q = isset($_GET['q']) ? strtolower($_GET['q']) : '';
    $date_from = strtotime($_GET['dfr']);
    // + 86400 seconds to include the end-date
    $date_to = strtotime($_GET['dto']) + 86400;
    
    // initialize $matches with nothing
    $matches = null;
    
    // search for matches between from_date and to_date
    if($date_from && $date_to)
        $matches = array_filter(scandir($dir),
        function($file) use($date_from, $date_to, $q) {
            $ctime = filectime($file);
            $lcfile = strtolower($file);
    
            // exclude entries that starts with '.'
            return $lcfile[0] !== '.' &&
                // just show PDF files
                substr($lcfile, -4) === '.pdf' &&
                // check search with filedate
                $ctime >= $date_from &&
                $ctime < $date_to;
        });
    // search for matches in filename
    else if($q)
        $matches = array_filter(scandir($dir),
        function($file) use($q) {
            $lcfile = strtolower($file);
    
            // exclude entries that starts with '.'
            return $lcfile[0] !== '.' &&
                // just show PDF files
                substr($lcfile, -4) === '.pdf' &&
                // check search with filename
                strpos($lcfile, $q) !== false;
        });
    
    if(empty($matches)) {
        echo "<p><em>Nothing Found</em></p>";
    } else {
        echo '<table cellpadding="5">
        <tr>
            <th>Invoice Number</th>
            <th>Date</th>
            <th>Click to View</th>
            <th>Download PDF</th>
        </tr>';
    
        foreach($matches as $file) {
            $fileDate = date("Y-m-d", filectime($file));
            echo "<tr>
                <td>$file</td>
                <td align=\"center\">$fileDate</td>
                <td align=\"center\"><a href='$dir$file'>View</a></td>
                <td align=\"center\"><a href='pdf_server.php?file=$file'>Download</a></td>
            <tr>";
        }
    
        echo '</table>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化