使用PHP可下载的大型远程映像 [英] Downloadable large remote images using PHP

查看:162
本文介绍了使用PHP可下载的大型远程映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的帖子,但在爬行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;

这样可以工作,但是有一个更好的方法,因为这个脚本可能会看到很多流量 - 也许



图片将从同一网络上的服务器提供。 >解决方案

10mb 非常大, 300个并发用户有10个请求



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



我建议您使用工作队列



您可以使用 Gearman

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

您不能使用AJAX并检查图片是否已下载并显示



我还建议您查看以下




I know there are lots of posts similar to this, but after crawling SO, still not found the answer.

I am looking to write a script that acts as a proxy for downloading large remote images (around 10mb each). So far I am using curl to read in the remote image url and then using headers to force a download. Something like (not the full script):

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;

This works, but is there a better way as this script may see quite a lot of traffic - maybe 300 concurrent users with 10 requests each?

The images will be served from a server on the same network.

解决方案

10mb is pretty large with 300 concurrent users with 10 requests.

You are saying 10 * 300 * 10 = 30,000 MB = 30GB

I Suggest you use a Job Queue

You can use Gearman

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

You can not use AJAX and to check if the image is downloaded and display it

I would also recommend you look at the following

这篇关于使用PHP可下载的大型远程映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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