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' =&gt; 1,
 'uri' =&gt; $filepath,
 'filemime' =&gt; file_get_mimetype($filepath),
 'status' =&gt; 1,
);

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

$node-&gt;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?
You can just use file_copy or file_move.

Here is an example: https://stackoverflow.com/questions/5129559/attaching-files-to-nodes-programmatically-in-drupal-7