使用PHP从远程服务器下载多个图像(大量的图像)
我试图从外部服务器下载大量文件(大约3700张图片)。
I am trying to download lots of files from an external server (approx. 3700 images). These images go from 30KB to 200KB each.
当我在图片上使用 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
。
这是我试过的代码:
wget:
exec('wget http://mediaserver.centris.ca/media.ashx?id=ADD4B9DD110633DDDB2C5A2D10&t=pi&f=I -O SIA/8605283.jpg');
copy:
if(copy($donnees['PhotoURL'], $filetocheck)) {
echo 'Photo '.$filetocheck.' updated<br/>';
}
cURL:
$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.
非常感谢, Antoine
Thanks a lot, Antoine
我使用了这个函数,效果非常好。
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');
}