cURL开始下载需要很长时间 [英] cURL takes long time to start download

查看:257
本文介绍了cURL开始下载需要很长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过服务器B下载服务器C上的文件(1GB),该文件具有代码:

I'am trying to download a file (1GB) on server C via server B, which has the code:

header("Content-Disposition: attachment; filename=How to Use Git and GitHub Videos.zip");
header("Content-type: application/octet-stream");
header("Content-Transfer-Encoding: binary");

$url = "http://zips.udacity-data.com/ud775/How%20to%20Use%20Git%20and%20GitHub%20Videos.zip";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);

curl_exec ($ch);
curl_close($ch);



我希望它会立即启动dwonlaoding文件。但是,我必须等待这么长时间,直到我的浏览器窗口会问,在哪里保存它。我认为 curl_setopt($ ch,CURLOPT_BUFFERSIZE,256); 意味着它将通过chunk传输文件chunk,所以我不会等待它将首先读取所有文件。

I was expecting it will momentally start dwonlaoding a file. But instead I have to wait so long time till my browser window will ask where to save it. I thought curl_setopt($ch, CURLOPT_BUFFERSIZE, 256); means that it will transfer the file chunk by chunk so i don'thave to wait it will read the all file first.

那么为什么需要这么长的时间才能开始下载?我该如何解决?如果这个方法是错误的,请建议我别的

So why it takes so long time to start download? How can I fix it? if this method is wrong please suggest me something else

推荐答案

这是因为 curl_exec $ c>阻塞,直到它读取完全响应(消耗1GB的内存在你的情况下的过程)。您可以调用自己的函数,但是:

That's because curl_exec() blocks till it reads full response (consuming 1GB of memory in the process in your case). You can make it call your own function however:

curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $buffer) {
  echo $buffer;
  return strlen($buffer);
});
curl_exec ($ch); // curl will call CURLOPT_WRITEFUNCTION as it receives data inside curl_exec()

确保 CURLOPT_RETURNTRANSFER 未设置,既不设置为false,也不设置为true。

Be sure CURLOPT_RETURNTRANSFER is not set at all, neither to false, neither to true.

这篇关于cURL开始下载需要很长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆