Code for resumable download


Another piece of code for PHP developers, useful in open source applications development.

Following is the function for application that needs functionality of resumable download.

public static function dl_file_resumable($file, $is_resume=TRUE)
//First, see if the file exists
if (!is_file($file))
die(“404 File not found!“);

//Gather relevent info about file
$size = filesize($file);
$fileinfo = pathinfo($file);

//workaround for IE filename bug with multiple periods / multiple dots in filename
//that adds square brackets to filename – eg. becomes setup[1].abc.exe
$filename = (strstr($_SERVER[‘HTTP_USER_AGENT’], ‘MSIE’)) ?
preg_replace(‘/\./’, ‘%2e’, $fileinfo[‘basename’], substr_count($fileinfo[‘basename’], ‘.’) – 1) :

$file_extension = strtolower($path_info[‘extension’]);

//This will set the Content-Type to the appropriate setting for the file
case ‘zip’: $ctype=’application/zip’; break;
default: $ctype=’application/force-download’;

//check if http_range is sent by browser (or download manager)
if($is_resume && isset($_SERVER[‘HTTP_RANGE’]))
list($size_unit, $range_orig) = explode(‘=’, $_SERVER[‘HTTP_RANGE’], 2);
if ($size_unit == ‘bytes’)
//multiple ranges could be specified at the same time, but for simplicity only serve the first range
list($range_from, $extra_ranges) = explode(‘,’, $range_orig, 2);
$range_from = ”;
$range_from = ”;

//figure out download piece from range (if set)
list($seek_start, $seek_end) = explode(‘-‘, $range_from, 2);

//set start and end based on range (if set), else set defaults
//also check for invalid ranges.
$seek_end = (empty($seek_end)) ? ($size – 1) : min(abs(intval($seek_end)),($size – 1));
$seek_start = (empty($seek_start) || $seek_end 0 || $seek_end < ($size – 1))
header(‘HTTP/1.1 206 Partial Content’);

header(‘Accept-Ranges: bytes’);
header(‘Content-Range: bytes ‘.$seek_start.’-‘.$seek_end.’/’.$size);

//headers for IE Bugs (is this necessary?)
//header(“Cache-Control: cache, must-revalidate”);
//header(“Pragma: public”);

header(‘Content-Type: ‘ . $ctype);
header(‘Content-Disposition: attachment; filename=”‘ . $filename . ‘”‘);
header(‘Content-Length: ‘.($seek_end – $seek_start + 1));

//open the file
$fp = fopen($file, ‘rb’);
//seek to start of missing part
fseek($fp, $seek_start);

//start buffered download
//reset time limit for big files
print(fread($fp, 1024*8));


about the author


  1. %

    November 22, 2011

    inch you’ll find it reestablish to mind in regards to the time I ran into my husband.

  2. patroness

    November 27, 2011

    I must appreciate this tries you get in establishing the next few paragraphs. I will be believing identical best work within you when you need it in addition

  3. ugg sale online

    December 2, 2011

    Hello dude! Dominatrix are my passion, please write way more i will arrive back daily to be sure of you !

  4. ugg classic mini boots

    December 6, 2011

    In the event you overpay ones taxes over the years, you’ll certainly be presenting the costa rica government a no cost personal loan.

  5. brazilian blowout good hair

    December 17, 2012

    There are certainly plenty of particulars like that to take into consideration. That could be a great level to convey up. I offer the thoughts above as normal inspiration however clearly there are questions just like the one you deliver up the place the most important factor shall be working in sincere good faith. I don?t know if greatest practices have emerged round things like that, but I am sure that your job is clearly recognized as a good game. Each boys and girls feel the impression of just a second’s pleasure, for the remainder of their lives.