使用PHP可下载的大型远程图像

时间:2012-10-08 14:16:05

标签: php curl stream download

我知道有很多类似的帖子,但在抓取SO之后,仍然没有找到答案。

我希望编写一个脚本,作为下载大型远程图像(每个大约10mb)的代理。到目前为止,我使用curl读取远程图像URL,然后使用标头强制下载。像(不是完整的脚本):

function getRemoteFile($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

header('Content-Type: octet/stream');
header('Content-Disposition: attachment; filename="random.jpg"');
header('Content-Length: ' . strlen($file));
echo $file;

这有效,但是有更好的方法,因为这个脚本可能会看到相当多的流量 - 可能是300个并发用户,每个有10个请求?

图像将从同一网络上的服务器提供。

1 个答案:

答案 0 :(得分:1)

10mb非常大300 concurrent users with 10 requests

您说10 * 300 * 10 = 30,000 MB = 30GB

我建议您使用作业队列

您可以使用Gearman

$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("download", "getRemoteFile");
while ($worker->work());

您无法使用AJAX并检查图像是否已下载并显示

我还建议您查看以下内容