We are using a PHP scripting for tunnelling file downloads, since we don't want to expose the absolute path of downloadable file:
header("Content-Type: $ctype");
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=\"$fileName\"");
readfile($file);
Unfortunately we noticed that downloads passed through this script can't be resumed by the end user.
Is there any way to support resumable downloads with such a PHP-based solution?
You could use the below code for byte range request support across any browser
This worked very well for me: https://github.com/pomle/php-serveFilePartial
Yes. Support byteranges. See RFC 2616 section 14.35 .
It basically means that you should read the
Range
header, and start serving the file from the specified offset.This means that you can't use readfile(), since that serves the whole file. Instead, use fopen() first, then fseek() to the correct position, and then use fpassthru() to serve the file.
If you're willing to install a new PECL module, the easiest way to support resumeable downloads with PHP is through
http_send_file()
, like thissource : http://www.php.net/manual/en/function.http-send-file.php
We use it to serve database-stored content and it works like a charm !
A really nice way to solve this without having to "roll your own" PHP code is to use the mod_xsendfile Apache module. Then in PHP, you just set the appropriate headers. Apache gets to do its thing.
The top answer has various bugs.
bytes a-b
should mean[a, b]
instead of[a, b)
, andbytes a-
is not handled.Here's my modified code: