save multiple external images

Error message

  • Deprecated function: Methods with the same name as their class will not be constructors in a future version of PHP; GeSHi has a deprecated constructor in require_once() (line 818 of /home/xenyomed/public_html/sites/all/modules/libraries/libraries.module).
  • Deprecated function: Function create_function() is deprecated in GeSHi->_optimize_regexp_list_tokens_to_string() (line 4736 of /home/xenyomed/public_html/sites/all/libraries/geshi/geshi.php).
  • Deprecated function: Methods with the same name as their class will not be constructors in a future version of PHP; ctools_context has a deprecated constructor in require_once() (line 127 of /home/xenyomed/public_html/sites/all/modules/ctools/ctools.module).
  • Deprecated function: Methods with the same name as their class will not be constructors in a future version of PHP; ctools_context_required has a deprecated constructor in require_once() (line 127 of /home/xenyomed/public_html/sites/all/modules/ctools/ctools.module).
  • Deprecated function: Methods with the same name as their class will not be constructors in a future version of PHP; ctools_context_optional has a deprecated constructor in require_once() (line 127 of /home/xenyomed/public_html/sites/all/modules/ctools/ctools.module).
  • Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in _menu_load_objects() (line 579 of /home/xenyomed/public_html/includes/menu.inc).

WE HAVE MOVED!

Please visit our new Drupal Website at https://www.xenyo.com.

<?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