3

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.

1 Answer 1

2

Finally have figured out. The following code works fine:

$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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.