I have a remote image URL address saved in $image_url variable and the following code to programmatically create a node:

$node = new stdClass();
  $node->uid	  = '1';
  $node->type     = 'article';
  node_object_prepare($node);
  $node->title    = $title;
  $node->status   = 1;
  $node->promote  = 0;
  $node->sticky   = 0;
  $node->language = 'ru';
$node->body['und'][0]['value']   = $body;
$node->body['und'][0]['format']  = 'filtered_html';
node_save($node);

Calling filefield_sources_save_file($image_url) in a custom module gives:

Warning: filesize(): stat failed for http://www.remotesite.com/images/2014/11/1414790760.jpg in filefield_sources_save_file() (line 451 of /home/mysite/public_html/sites/all/modules/filefield_sources/filefield_sources.module).
The specified file 1414790760.jpg could not be uploaded. Only files with the following extensions are allowed: jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp.

Could you please help me to make changes into above peace of code to programmatically create a node with the image in the imagefield saved from a remote source.

Comments

yngens2’s picture

Had to take different path with file_save_data():

$file = file_save_data(file_get_contents($image_url), file_default_scheme().'://tmp/'.basename($image_url));
$file->status = 1;
$node->field_image['und'][0] = (array)$file;

node_save($node);

Let me know if there is nicer way of doing this with filefield_sources_save_file().