How can I change an image field programatically? I'm trying to change the image field 'Badge' when an article is situated at the top of a view 'Top 3' after it has been updated. This is my custom module:

function top_node_update($node) {
    $top = views_get_view_result('top_3');
    $first = $top[0]-&gt;nid;

    if ($first == $node-&gt;nid) {
        $node-&gt;field_badge[0]['value'] = '/default/files/images/gouden_rozet.png';
        field_attach_update('node', $node);
    }
}


Unfortunately, nothing happens when I edit and save an article.

What's going wrong and how can I solve this?



Edit: Everything seemed perfectly fine after @undersounds answer, because the image fields were correctly changed by my custom module. I used the same piece of code to also edit the images of the second and third node in the view:

&lt;?php

function top_node_update($node) {
  $top = views_get_view_result('top_3');
  $first = $top[0]-&gt;nid;
  $second = $top[1]-&gt;nid;
  $third = $top[2]-&gt;nid;

  if ($first == $node-&gt;nid) {
    $image = file_get_contents('public://images/gouden rozet.png');
    $file  = file_save_data($image, 'public://images/gouden rozet.png',FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }

  if ($second == $node-&gt;nid) {
    $image = file_get_contents('public://images/zilveren rozet.png');
    $file  = file_save_data($image, 'public://images/zilveren rozet.png',FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }

  if ($third == $node-&gt;nid) {
    $image = file_get_contents('public://images/bronzen rozet.png');
    $file  = file_save_data($image, 'public://images/bronzen rozet.png', FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }
}


I started playing around a bit to see whether everything worked and discovered this: when a certain node has for example the 'bronzen rozet' image and moves to the first row in the view, the image is changed to 'gouden rozet' - as it should be - but the bronzen rozet.png file is also deleted in my images file!

How can I prevent this?
function top_node_update($node) {
      $top = views_get_view_result('top_3');
      $first = $top[0]-&gt;nid;
      $second = $top[1]-&gt;nid;
      $third = $top[2]-&gt;nid;

  if ($first == $node-&gt;nid) {
    $image = file_get_contents('public://images/badges/gouden rozet.png');
    $file  = file_save_data($image, 'public://images/gouden rozet.png',FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }

  if ($second == $node-&gt;nid) {
    $image = file_get_contents('public://images/badges/zilveren rozet.png');
    $file  = file_save_data($image, 'public://images/zilveren rozet.png',FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }

  if ($third == $node-&gt;nid) {
    $image = file_get_contents('public://images/badges/bronzen rozet.png');
    $file  = file_save_data($image, 'public://images/bronzen rozet.png', FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }
}


You should also try to prevent using functionality twice. So instead of the code above you can also write something like:

function top_node_update($node) {

  $top = views_get_view_result('top_3');
  $first = $top[0]-&gt;nid;
  $second = $top[1]-&gt;nid;
  $third = $top[2]-&gt;nid;

  if ($first == $node-&gt;nid) {
    $img_name = 'gouden rozet.png';
  }

  if ($second == $node-&gt;nid) {
    $img_name = 'zilveren rozet.png';
  }

  if ($third == $node-&gt;nid) {
    $img_name = 'bronzen rozet.png';
  }

  $image = file_get_contents('public://images/badges/' . $img_name);
  $file  = file_save_data($image, 'public://images/' . $img_name, FILE_EXISTS_REPLACE);
  $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
  field_attach_update('node', $node);


}