Programmatically attaching image files to nodes in Drupal 7

I found the answer on Stackoverflow.

Simple, right? And this works fine if you’re attaching a brand new image.

I found the answer on Stackoverflow

I found the answer on Stackoverflow.

Simple, right? And this works fine if you’re attaching a brand new image. However, if you’re updating an image you’ll find that the styles don’t update after calling node_save(), and the image will not show up correctly on your site. You need one extra step: call image_path_flush() to regenerate that image’s styles before calling node_save().

//if the image is being updated, flush all its styles
image_path_flush($node->field_image['und'][0]->uri);