I am using the following php function to download bulk remote images in parallel. it is fast enough to fetch remote images to my server.
function save($urls){
$save_to=__DIR__.'/thumb/';
$conn = array();
$fp = array();
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
$g=$save_to.basename($url);
if(!is_file($g)){
$conn[$i]=curl_init($url);
$fp[$i]=fopen ($g, "wb");
curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
curl_setopt ($conn[$i], CURLOPT_MAXCONNECTS, 10);
curl_multi_add_handle ($mh,$conn[$i]);
}
}
$active = null;
do {
curl_multi_exec($mh,$active);
}
while ($active > 0);
foreach ($urls as $i => $url) {
curl_multi_remove_handle($mh,$conn[$i]);
curl_close($conn[$i]);
fclose ($fp[$i]);
}
curl_multi_close($mh);
}
the problem here is that i am getting the following Warnings and notices:
Notice: Undefined offset: 0 in C:\wamp\www\test\images.php on line 103
Warning: curl_multi_remove_handle() expects parameter 2 to be resource, null given in C:\wamp\www\test\images.php on line 103
Notice: Undefined offset: 0 in C:\wamp\www\test\images.php on line 104
Warning: curl_close() expects parameter 1 to be resource, null given in C:\wamp\www\test\images.php on line 104
Notice: Undefined offset: 0 in C:\wamp\www\test\images.php on line 105
those alarms are appearing for each image fetch.
any idea on how to eliminate such alarm causes?
Thanks you
EDIT: i have followed lafor suggestion and changed the last foreach to:
foreach ($conn as $i => $url) {
...
}
the number of alarms got decreased but still getting the same alarms at the following two lines:
curl_multi_remove_handle($mh,$conn[$i]);
curl_close($conn[$i]);