File Download with From date and To date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yudha2008
Forum Newbie
Posts: 5
Joined: Tue Oct 20, 2009 1:47 am

File Download with From date and To date

Post by yudha2008 »

Hi,

Ii have stuck up with one coding. I want to downloads the file From particular folder. It is in this format name-dateandtime.format

Examples : voicefile-2009-10-14.doc or 5101-2009-10-14.gsm

I can able to get file download for particular date. But how can i get from particular date to some date

Example : From date : 2009-10-14
To date : 2009-10-20

I want to get data or file to be downloaded for these dates. How i can i able to get it for particular dates.

can any one assist me with some sample code or some examples for this process.

My code: (For particular date)

<?php
include("conn.php");
$vpath = "/var/lib/";
?>
<form method=post action='<?php $_SERVER[PHP_SELF] ?>' name="form" onsubmit =validateForm();>
<center>
<table cellpadding="0" cellspacing="0" border="0">
<!--<tr><td>Phone Number:</td><td><input type="text" name="campaign" value="" /></td>
<td><input type="submit" name="search1" value="Search" class="butt" /></td></tr>-->
<tr valign="top"><td valign="middle">Date:</td>
<td><input type="Text" id="demo2" name ="phone_no" > <a href="javascript:NewCal('demo2','ddmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date" align="top"></a></td></tr><br/>
<tr>
<td></td>
<td align="center"><input type="submit" name="search3" value="Search" class="butt" /></td>
</tr>
</table>
</center>

</form>
<?
$campaign=$_POST['campaign'];
$phone = $_POST['phone_no'];
$agentid=$_POST['agentid'];
$submit3 = $_POST['search3'];


if($submit3 && $phone){
if ($handle = opendir($vpath)) {

echo "<DIV id='layout1'><center><table cellspacing=0 cellpadding=0 border=5 id='thetable'>
Voice File Status For - ". $phone."
<tr><th class='th'>Date</th><th class='th'>Filename</th><th class='th'>Download</th></tr>";

$i=0;
while (false !== ($file1 = readdir($handle)))
{

$file = explode(".",$file1);


//$atime = date("d-m-Y", filemtime($file[0]));

$fname =$vpath.$file1;


$atime = date("d-m-Y", filemtime($fname));

//echo $atime;
if($atime==$phone)
{

echo "<tr align=center><td>$atime</td><td>$file1</td><td align='center'><a href='download.php?path=$vpath&file=$file1'><img src='images/pointer_down.gif' border=0></a></td></tr>";

}


$i++;

}
echo "</table></center></DIV>";
closedir($handle);
}

}

?>


[/b]
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: File Download with From date and To date

Post by Mark Baker »

If you want more than one file to be downloaded in one go, you'd need to select all the files that fell within the data range and zip them up, then download the zip
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: File Download with From date and To date

Post by requinix »

If you're wondering about how to know what files are in that range I've got one word for you: wildcards. glob can help with that.

But it's a bit tricky so I'll give code.

Code: Select all

// return a list of all files where the date in the filename is between two
// dates. all files must look like "*YYYY-MM-DD.extension"
// $path = path to the files
// $pre = anything before the date in the filename
// $date1 = date range start as YYYY-MM-DD
// $date2 = date range end as YYYY-MM-DD
function filesbetweendates($path, $pre, $date1, $date2) {
    $len = min(strlen($date1), strlen($date2));
    for ($i = 0; $i < $len && $date1[$i] == $date2[$i]; $i++);
 
    $pattern = ($i > 0
        ? $path . $pre . substr($date1, $i) . "*.*"
        : $path . $pre . "*.*"
        );
    $files = glob($pattern);
 
    $return = array();
    foreach ($files as $file) {
        $noext = basename($file, strrchr($file, "."));
        $date = substr($noext, -10);
        if ($date1 <= $date && $date <= $date2) $return[] = $file;
    }
    return $return;
}
 
// call it like
$files = filesbetweendates("/path/to/files/", "*-", "2009-10-14", "2009-10-20");
Then loop over $files and do whatever you want.


How it works:
It figures out how similar the dates are and adds a wildcard after the common part. 2009-10-14 and 2009-10-20 share the "2009-10-" in common, and the file pattern given to glob looks like "2009-10-*". It then checks each file to make sure the date is correct.
yudha2008
Forum Newbie
Posts: 5
Joined: Tue Oct 20, 2009 1:47 am

Re: File Download with From date and To date

Post by yudha2008 »

Hi tasairis,

I have tried this coding but i cant able to get files in between dates . I can able to get the files for the single date only.

I can't able to get in between date files.

:crazy:

Thanks with Regards,

yudha
Post Reply