Dear friend,
I 've figure out how to do this. Hope the information I provide below
could be
helpful.
Please reference to http 1.1's RFC document ----rfc 1068.
I suggest you to put your files on your file system out of the Document root
so that unauthorized users can't access them. and decrease overload
of your database, and make it more convinent for implement "resume" feaure.
Below is how. (code not optimized)
And in your script , give the brower appropriate headers like this:
<?
..................verify user......................
.....................
//get all http header
$headers = getallheaders();
while (list($header, $value) = each($headers))
{
$header_str.="$header: $value\r\n";
}
//parse the http header to get the RANGE value
$pos_begin=strpos($header_str,"bytes=");
$range_byte=0;
if ($pos_begin<>false)
{
$pos_end=strpos($jhp,"-",$pos_begin);
$pos_begin+=6;
$range_byte=substr($jhp,$pos_begin,$pos_end-$pos_begin);
............open the file, get its length...............
//figure out the real length of the bytes that your sciprt will send out
$real_length=$file_length-$range_byte;
$real_length=$file_length-$range_byte;
if ($pos_begin<>false)
{
header("HTTP/1.1 206 Partial Content");
}
header("Accept-Ranges: bytes");
header("Content-Type:Application/Octet-Stream");
//you can also use specific MIME types like " images/gif" or "Application/msword"
header("content-disposition:attachment;filename=$file_name");
header("content-description:php3 generated data")
$range_response="Content-range: bytes ".$range_byte."-";
$range_response.=$file_length-1;
$range_response=$range_response."/";
$range_response=$range_response.$file_length;
header($range_response);
..........
..........
$fp=fopen($file,"r");
fseek($fp,$range_byte);
while ( $result=fread($fp,10000) )
{
//sleep(1);
echo $result;
}
fclose($fp);
?>
Please use a fancy URL to lead the user to this script, use something like:
<a href="scirpt.php3?/realname.xml">download this file</a>.
The reason for this is : downloading tools such as Net Ant can truncate
a mile long URL to a reasonable name like "realname.xml" .
Because my ( and your's) script has to support these kind of downloading tool,
which have the greate feature of "resume" or "multi point resume",
so it's very important for them to echo out appropriate RANGE with the header
function.
And it's necessary to let your script to sleep a while ( e.g. 1 second ) to let the
Web Server serve other users (if your files are big).
Also, if you use the powerful downloading tool, the Net Ant, you can see how
it talk with the web server or your scripts.