help needed to merge two scripts
Posted: Thu May 17, 2007 8:23 am
Hello, I have a script for downloading files, as shown below. It doesnt support download resuming tho. So i found another script but it comes with a download speed limiter. Can anyone show me where I can put the second script into the first so that download resuming can happen??
Thanks!
I basically need to allow download resuming on the first script which i think has something to do with: $_SERVER['HTTP_RANGE']
Script 1 - Original Script
Second script - Download resuming allowed:
Thanks for youre help.
Thanks!
I basically need to allow download resuming on the first script which i think has something to do with: $_SERVER['HTTP_RANGE']
Script 1 - Original Script
Code: Select all
<?
function dl_file($file){
//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }
//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "mp3": $ctype="audio/mpeg"; break;
case "wav": $ctype="audio/x-wav"; break;
case "mpeg":
case "mpg":
case "mpe": $ctype="video/mpeg"; break;
case "mov": $ctype="video/quicktime"; break;
case "avi": $ctype="video/x-msvideo"; break;
//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
case "php":
case "htm":
case "html":
case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
default: $ctype="application/force-download";
}
//Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
//Use the switch-generated Content-Type
header("Content-Type: $ctype");
//Force the download
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($file);
exit;
}
$file=$_GET['file'];
dl_file('D:/inetpub/files/xxxxx/' .$file);
?>Second script - Download resuming allowed:
Code: Select all
<?php
/**
* Bandwidth Throttling
*
* You can enable this option to throttle your
* downloads. To throttle, just configure this
* line with your prefered setting.
*
* In the future, you will be able to throttle
* on a per file basis.
*/
// Set to TRUE to enable Bandwidth Throttling
$bandwidth_throttle = TRUE;
// The number (In Kilobytes) to send per Download
// Default is 10 KB/s.
$bandwidth_speed_kb = 5;
/**
* End Of Configuration
*
* No need to edit the below code.
*/
function get_download_range($input, $filesize)
{
if (!empty($input) && (strncasecmp("bytes=", $input, 6) == 0))
{
$input = substr($input, 6);
$ranges = explode(",", $input);
$explicit_range = TRUE;
}
else
{
$ranges = array();
$explicit_range = FALSE;
}
if (count($ranges) == 1)
$range = trim($ranges[0]);
else
$range = "-";
$bounds = explode("-", $range);
if (empty($bounds[0]))
$bounds[0] = 0;
if (empty($bounds[1]))
$bounds[1] = $filesize-1;
return array($bounds[0], $bounds[1], $explicit_range);
}
$file = ("C:\Users\Public\Videos\Lost\"" . $_GET['get']);
$file_name = basename($file);
$type = "application/force-download";
if (!file_exists($file))
{
header("HTTP/1.1 404 Not Found");
echo "File Not Found";
exit;
}
$length = filesize($file);
$r = get_download_range($_SERVER['HTTP_RANGE'], $length);
$start = $r[0];
$stop = $r[1];
$partial_length = $stop - $start + 1;
if ($r[2])
{
header("HTTP/1.1 206 Partial Content");
header("Content-Range: bytes $start-$stop/$length");
}
else
{
header("HTTP/1.1 200 OK");
}
header("Content-Type: $type");
header("Content-Length: $partial_length");
header("Content-Disposition: attachment; filename=\"$file_name\"");
$fp = fopen($file, 'rb');
fseek($fp, $start);
while ($partial_length > 0)
{
if ($bandwidth_throttle)
$buffer_size = $bandwidth_speed_kb * 1024;
else
$buffer_size = 16 * 1024;
if ($buffer_size > $partial_length)
$buffer_size = $partial_length;
print fread($fp, $buffer_size);
$partial_length -= $buffer_size;
if ($bandwidth_throttle && ($partial_length > 0))
{
ob_flush();
sleep(1);
}
}
fclose($fp);
?>