The code is as follows:
function Remote ($urls, $name = ', $path = ', $dir = './images/') {
if (!is_array ($urls) or count ($urls) = = 0) {
return false;
}
Dmkdir ($dir);
$curl = $text = Array ();
foreach ($urls as $k => $v) {
if (!empty ($v) && preg_match ("~^http~i", $v)) {
$nurl [$k] = Trim (Str_replace (",%20", $v));
$curl [$k] = Curl_init ($nurl [$k]);
curl_setopt ($curl [$k], curlopt_ipresolve, CURL_IPRESOLVE_V4);
curl_setopt ($curl [$k], Curlopt_returntransfer, 1);
curl_setopt ($curl [$k], Curlopt_header, 0);
curl_setopt ($curl [$k], curlopt_connecttimeout, 20);
if (!isset ($handle)) {
$handle = Curl_multi_init ();
}
Curl_multi_add_handle ($handle, $curl [$k]);
}
continue;
}
$active = null;
do{
$MRC = @curl_multi_exec ($handle, $active);
} while ($MRC = = Curlm_call_multi_perform);
while ($active && $MRC = = CURLM_OK) {
if (Curl_multi_select ($handle)!=-1) {
do {
$MRC = curl_multi_exec ($handle, $active);
} while ($MRC = = Curlm_call_multi_perform);
}
}
foreach ($curl as $k => $v) {
if (Curl_error ($curl [$k]) = = "") {
if ($k = = 0) {
$fname [$k] = Strtolower ($name. '.' . PathInfo ($urls [$k], pathinfo_extension));
} else {
$fname [$k] = Strtolower ($name. '_' . $k. '.' . PathInfo ($urls [$k], pathinfo_extension));
}
$text [$k] = (string) curl_multi_getcontent ($curl [$k]);
$filedir [$k] = $dir. '/'. $fname [$k];
if (File_put_contents ($filedir [$k], $text [$k])) {
$filepath [$k] = $path. $fname [$k];
}
}
Curl_multi_remove_handle ($handle, $curl [$k]);
curl_close ($curl [$k]);
}
Curl_multi_close ($handle);
return $filepath;
}