使用 PHP 从远程服务器下载多个图像(大量图像)
我正在尝试从外部服务器下载大量文件(大约 3700 个图像).这些图片每个大小从 30KB 到 200KB.
I am trying to download lots of files from an external server (approx. 3700 images). These images go from 30KB to 200KB each.
当我在 1 张图像上使用 copy()
函数时,它起作用了.当我循环使用它时,我得到的只是 30B 图像(空图像文件).
When I use the copy()
function on 1 image, it works. When I use it in a loop, all I get are 30B images (empty images files).
我尝试使用 copy
、cURL
、wget
和 file_get_contents
.每次,我要么得到很多空文件,要么什么都没有.
I tried using copy
, cURL
, wget
, and file_get_contents
. Every time, I either get a lot of empty files, or nothing at all.
这是我试过的代码:
wget:
exec('wget http://mediaserver.centris.ca/media.ashx?id=ADD4B9DD110633DDDB2C5A2D10&t=pi&f=I -O SIA/8605283.jpg');
复制:
if(copy($donnees['PhotoURL'], $filetocheck)) {
echo 'Photo '.$filetocheck.' updated<br/>';
}
卷曲:
$ch = curl_init();
$source = $data[PhotoURL];
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = $newfile;
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
似乎没有任何工作正常.不幸的是,我没有太多选择可以一次下载所有这些文件,我需要一种方法使其尽快运行.
Nothing seems to be working properly. Unfortunately, I don't have much choice to download all these files at once, and I need a way to make it work as soon as possible.
非常感谢,安东尼
我为此使用了这个功能并且效果很好.
I used this function for that and worked pretty well.
function saveImage($urlImage, $title){
$fullpath = '../destination/'.$title;
$ch = curl_init ($urlImage);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
$r = fwrite($fp, $rawdata);
setMemoryLimit($fullpath);
fclose($fp);
return $r;
}
结合另一个防止内存溢出:
Combined with this other one to prevent memory overflow:
function setMemoryLimit($filename){
set_time_limit(50);
$maxMemoryUsage = 258;
$width = 0;
$height = 0;
$size = ini_get('memory_limit');
list($width, $height) = getimagesize($filename);
$size = $size + floor(($width * $height * 4 * 1.5 + 1048576) / 1048576);
if ($size > $maxMemoryUsage) $size = $maxMemoryUsage;
ini_set('memory_limit',$size.'M');
}