0

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>         
KiD Cajes
  • 129
  • 8
  • take a look at this: https://stackoverflow.com/questions/3062154/php-list-of-specific-files-in-a-directory and this: http://php.net/manual/en/function.filemtime.php – popeye Jan 04 '18 at 10:41
  • I've already seen that and made it as one of my reference. – KiD Cajes Jan 04 '18 at 10:56

2 Answers2

0

Follow below code

1. Make a function to check image date is in range of from_date and to_date

 function isDateInRange($startDate, $endDate, $userDate)
  {
   $startT = strtotime($startDate);
   $endT = strtotime($endDate);
   $userT = strtotime($userDate);
   return (($userT >= $startT) && ($userT <= $endT));
}

2. Make function to get image correct Mtime

function GetCorrectMTime($filePath) {
            $time = filemtime($filePath);
            $isDST = (date('I', $time) == 1);
            $systemDST = (date('I') == 1);
            $adjustment = 0;
            if ($isDST == false && $systemDST == true)
                $adjustment = 3600;
            else if ($isDST == true && $systemDST == false)
                $adjustment = -3600;
            else
                $adjustment = 0;
            return ($time + $adjustment);
  }

3.Now logic to get list of files between from_date and to_date

        $date_from = '2017-01-01'; 
        $date_to = '2017-01-04'; 

            $log_directory = 'your directory path';
            $filedata = array();
            if (is_dir($log_directory)) {
                if ($handle = opendir($log_directory)) {
                    //Notice the parentheses I added:
                    while (($file = readdir($handle)) !== FALSE) {
                        if ($file != '.' && $file != '..') {

                            $checkdate = date('Y-m-d',$this->GetCorrectMTime($log_directory . $file));
                            if(isDateInRange($date_from,$date_to,$checkdate)){

                                $filedata[] = array('filename' => $file, 'filetime' => $this->GetCorrectMTime($log_directory . $file), 'filepath' => $log_directory . $file);
                            }
                        }
                    }
                    closedir($handle);
                }
            }

        print_r($filedata); //array containg list of files between from_date and to_date
Dharmendra Singh
  • 1,186
  • 12
  • 22
0

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>';
}
Steven
  • 381
  • 2
  • 10
  • Yes it solved the date range that I'm looking for but the capability of show the files when $q is searched don't. And if the user select 12-29-2017 to 12-31-2017 it only displays 12-29-2017 files though there are files for 12-31. – KiD Cajes Jan 04 '18 at 11:57
  • I've missed the check for 0-indexed match of $q and I didn't include the $end_date in search-results... $q-search should now work as expected. – Steven Jan 04 '18 at 12:16
  • Now it should be perfect for your use-case: it just runs the search, when you type in $q and the search-results includes uppercase file-extensions. – Steven Jan 04 '18 at 12:39
  • I added the form code from my main.html. I also used your latest code but it's only returning a blank page. – KiD Cajes Jan 07 '18 at 06:13
  • I've edit the example to match your forms. Bevore the description of your problem was to me, that you search for a file based on date and string. – Steven Jan 07 '18 at 18:15