Error programmaticaly adding image to node using file_copy I am trying to add an image to the article content type in Drupal 7, When I create a simple note i.e. article it works , but when I try to add the image to the article it gives an error 


  The specified file
  E:\WS_IntPOC_Test4\sites\all\modules\my_module\errorwinmobilesdk.jpg
  could not be copied, because the destination is invalid. More
  information is available in the system log


In log I can see the message as 


  File
  E:\WS_IntPOC_Test4\sites\all\modules\my_module\errorwinmobilesdk.jpg
  could not be copied, because the destination is invalid. This is often
  caused by improper use of file_copy() or a missing stream wrapper.


I have checked the wrapper public and private is there. As if I created the Article from GUI. It is created with image loaded in public folder. Please help 

$body_text = 'node body text';

  $node = new stdClass();
  $node-&gt;type = 'article';
  node_object_prepare($node);

  $node-&gt;title    = 'Node Created Programmatically on ' . date('c');
  $node-&gt;language = LANGUAGE_NONE;

  $node-&gt;body[$node-&gt;language][0]['value']   = $body_text;
  $node-&gt;body[$node-&gt;language][0]['summary'] = text_summary($body_text);
  $node-&gt;body[$node-&gt;language][0]['format']  = 'filtered_html';



// Some file on our system

$path ='E:\WS_IntPOCTest4\sites\all\modules\my_module\errorwinmobilesdk.jpg' ;
$file = (object) array(
          'uid' =&gt; 1,
          'uri' =&gt; $path,
          'filemime' =&gt; file_get_mimetype($path),
          'status' =&gt; 1,
 ); 

//$file = file_copy($file, 'public://'); // Save the file to the root of the files 
$file = file_copy($file);
$node-&gt;field_image[LANGUAGE_NONE][0] = (array)$file; 
//associate the file object with the image field:
node_save($node);
You can try this code:

//$filepath = drupal_realpath('misc/image.jpeg');
 [**UPDATED ANSWER**:]
 $filepath =  $_SERVER['DOCUMENT_ROOT'].base_path() .drupal_get_path('module','payment_details').'/MDSO_20.jpg';


$file = (object) array(
       'uid' =&gt; 1,
       'uri' =&gt; $filepath,
       'filemime' =&gt; file_get_mimetype($filepath),
       'status' =&gt; 1,
  );
  $file = file_copy($file, 'public://');
  $node-&gt;field_image[LANGUAGE_NONE][0] = (array)$file;
  node_save($node);


Hope this works for you :)

For me it created a node as you below :)