Skip to main content
3 of 4
added 68 characters in body
Sachin T
  • 67
  • 1
  • 8

Programmatically attach image to a content type from local system folder

I am in middle of coding creating nodes programatically. Here i have one problem. I need to programmatically attach image to the content type from local system folder. I mean my image path example 'c://xxxxx/xxxx/images/img.jpg'.

Thanks in advance for your advice.

Hi i used this code and work fine in the localhost. But it's failed when i work on the server.

$filepath = drupal_realpath('misc/druplicon.png');
// Create managed File object and associate with Image field.
$file = (object) array(
 'uid' => 1,
 'uri' => $filepath,
 'filemime' => file_get_mimetype($filepath),
 'status' => 1,
);

// We save the file to the root of the files directory.
$file = file_copy($file, 'public://');

$node->field_image[LANGUAGE_NONE][0] = (array)$file;

Reference link : attaching-files-to-nodes-programmatically-in-drupal-7

I didn't get the solution yet. Can any one have the experience?

Sachin T
  • 67
  • 1
  • 8