10 July 2011

Save file or image programmatically. First get file from directory or URL. Then save the image and assign it to the field you wish.

Source code viewer
  1. $image_url = 'http://example.com/image.png';
  2. $file = (array) system_retrieve_file($image_url, file_default_scheme().'://products_images/'.basename($image_url), TRUE);
  3. $node = node_load(1);
  4.  
  5. // Edit the array directly.
  6. $node->field_image[LANGUAGE_NONE][0] = $file;
  7. node_save($node);
  8.  
  9. // OR use entity metadata wrapper.
  10. $wrapper = entity_metadata_wrapper('node', $node);
  11. $wrapper->field_photo->file->set(array($file));
  12. $wrapper->save();
Programming Language: PHP