how to attach existing images(image field) to node in drupal 7? I use the following code to upload images

$node  = node_load($nid); 
$file  = (object) array(
  'uid' =&gt; $v['uid'],//@todo from query
  'uri' =&gt; 'public://'.$v['filename'],//@todo from query
  'filemime' =&gt; $v['filemime'],//@todo from query
  'status'   =&gt; $v['status'],//@todo from query
  'fid' =&gt; $v['fid'],
  'filesize' =&gt; $v['filesize'],
  'timestamp' =&gt; $v['timestamp'],
  'type' =&gt;'image',
  'metadata' =&gt; array('height' =&gt;1200,'width'=&gt;1099),
  'height' =&gt; 1200,
  'width'  =&gt; 1099,
);
$node-&gt;field_image[LANGUAGE_NONE][0] = (array) $file;
node_save($node);


This does not work, I am not able to figure out the error in the code which prevents from image upload
can u provide me an example of how to upload image programmatically
  provided I have an image url on the public path in D7


This should do the trick

$node  = node_load($nid);     
$image = file_get_contents('public://images/folder_name/image_name.jpg');
$file  = file_save_data($image, 'public://images/folder_name/image_name.jpg', FILE_EXISTS_REPLACE);
$node-&gt;field_image[LANGUAGE_NONE][0]['fid'] = $file-&gt;fid;
node_save($node);