I'm moving a bunch of info from an xml feed into drupal nodes. I'm a little hung up on adding the image. I'm using php copy() to copy the image from a url to my drupal directory. From here, I'm not sure how to insert the field info into the node in such a way that an fid and all of the required data is generated. What is the preferred way to do this?

1 Answer 1


OK, what I came up with here:

function save_my_image($uri) {
  //copy file into drupal files directory
  $contents = file_get_contents($uri);
  //then save into drupal's file system
  $file = file_save_data($contents);
  return $file;

file_save_data() (http://api.drupal.org/api/drupal/includes--file.inc/function/file_save_data/8) will handle creating an fid for the file and keeping track of it generally.. then simply add the fid to the node in question and you should be golden.

Your Answer

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

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