I want to "proxy" a file located on a remote server (let's call it Server B) and force its download to the visitor, from Server A.
Only Server A can access the ressource on Server B (IP address secured). The ressource can weight up to a few gigabytes.
Here is my code so far:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
$stream = fopen('php://output', 'w');
$ch = curl_init($ruri);
curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($stream) {
return fwrite($stream, fread($fd, $length));
});
curl_exec($ch);
curl_close($ch);
It works partially.
Problem number 1:
- The visitor cannot browse any page of the website on Server A while he is downloading the ressource. Why?
Problem number 2:
- It does not support pause or resume download. Apparently I can solve it using "range" headers. I did not try yet since "problem number 1" is a big problem, and I would like to solve it first before going further in that direction.
Please note:
- I do not want the ressource to be fully downloaded on Server A's disk before serving it to the visitor, I want it to be "forwarded" directly to the visitor (aka "proxy"?)
- I read posts about readfile() and file_get_content(), I tried, same problems. Not even mentionning how much these functions "stress" Server A (CPU, Memory), especially with big files.
- Isn't there a way to do it more efficiently? I mean without PHP, but rather trough Apache or Nginx?