douwen5681 2016-08-29 02:14
浏览 14

too long

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?
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料