save multiple external images

<?php
function modulename_get_remote_image($url) {
if (!$url) return FALSE;
if (preg_match('/([^\/]+)$/', $url, $matches)) {
$filename = $matches[1];
}
else{
return FALSE;
}
 
$filepath = file_directory_path().'/'.$filename;
if (file_exists($filepath))
return $filepath;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
if (file_save_data($data, '/'.$filename, TRUE))//replace
  return $filepath;
}
}
 
 url = 'http://www.xxx.com/xxx.jpg';
url2 = 'http://www.xxx.com/xxx2.jpg';
 $path = modulename_get_remote_image($url);
 $file = field_file_save_file($path);
 $path = modulename_get_remote_image($url);
 $file2 = field_file_save_file($path);
 
 $node->field_images[] = $file;
 $node->field_images[] = $file2;
node_save($node);
 
?>
Help Share this Article