How to upload/insert images in content from module? $article = entity_create('node', array(
    'type' =&gt; 'article',
    'title' =&gt; generate_demo_content('lorem_ipsum_title'),
    'body' =&gt; array(
      'value' =&gt; generate_an_article(),
      'format' =&gt; 'full_html',
    ),
    // how attach here, a file or an image?
  ));
  $article-&gt;save();


I create an article from a .module file, and I would like know if I can link an image/file object to my article?

By example if I have an image inside /modules/my_module_name/images/img.jpg, how I can insert this image inside my article object from my .module file please?

Thanks.
I have tried the below code with node_save method but not with entity_create way. Check if it works.

$dir = 'public://';
if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
  $file = file_save_upload('image', array(), $dir, FILE_EXISTS_RENAME);
  $file-&gt;status = FILE_STATUS_PERMANENT;
  file_save($file);
  $attachment = array(
    'fid' =&gt; $file-&gt;fid,
    'display' =&gt; 1,
    'description' =&gt; $file-&gt;name,
    'title' =&gt; $values-&gt;tooltip
  );
}

$article = entity_create('node', array(
  'type' =&gt; 'article',
  'title' =&gt; generate_demo_content('lorem_ipsum_title'),
  'body' =&gt; array(
    'value' =&gt; generate_an_article(),
    'format' =&gt; 'full_html',
  ),
  'field_attachment' =&gt; $attachment, //may need to change here
));