How do I programmatically add an image while programmatically creating nodes? I'm writing a script to add nodes programmatically, and I don't know the proper way to add/attach image. Actually I'm not that familiar with Drupal.

Here are the (sample) image objects I found while print_ring the existing $node I'm using:


field_image: Array ( [und] => Array ( [0] => Array ( [fxid] => 3089 [alt] => [title] => [width] => 95 [height] => 126 [uid] => 249 [filename] => helloworld.jpg [uri] => public://helloworld.jpg [filemime] => image/jpeg [filesize] => 3694 [status] => 1 [timestamp] => 1346748001 [type] => image [media_title] => Array ( ) [media_description] => Array ( ) [field_tags] => Array ( ) [field_license] => Array ( [und] => Array ( [0] => Array ( [value] => nothing ) ) ) [metatags] => Array ( ) [rdf_mapping] => Array ( ) ) ) )


The next one I found is the following.


field_temp_image: Array ( [und] => Array ( [0] => Array ( [value] => http://www.example.com/sample-path/helloworld.jpg [format] => [safe_value] => http://www.example.com/sample-path/helloworld.jpg ) ) ) `


How should I add an image to that node in that way?
Let suppose your image fields in field_body_images ,

first load your node by node_load
and record your image in file tables and add it to your node image field
I hope this sample help you to achieve :

$n=  node_load($nid);
$file = new stdClass();      
$file-&gt;filename =$file_name;
$file-&gt;filemime =file_get_mimetype($localimagepath.$file_name);
$file-&gt;filesize = @filesize(file_create_path($localimagepath.$file_name));
$file-&gt;uid = $user-&gt;uid;
$file-&gt;status = 1;
$file-&gt;timestamp = time();
$file-&gt;list=1;
$file-&gt;data=array('alt'=&gt;'','title'=&gt;$n-&gt;title);
drupal_write_record('files', $file);
$record-&gt;fid=$file-&gt;fid;
$n-&gt;field_body_images[]=(array)$file;
node_save($n);