Search code examples
drupaldrupal-7imagefield

Change the content of an image field


I just want to use node_save() to change value of an image field in my article, and I am using the following code.

$node = node_load(1);
$node->field_image["und"][0]["filename"] = $file_name;
node_save($node);

When I press F5, the image in my article is not changed.

Is anything wrong in my code?


Solution

  • I find the easiest way to deal with problems like these is to use the Devel module.

    In your code you could do this:

    $node = node_load(1);
    dsm($node);
    ....
    node_save($node);
    dsm($node);
    

    Devel dsm() function will output the node's properties & you will be able to check any values you would like to edit.

    In the example above, I can check before & after the edit. If everything looks correct, then it's just a question of clearing the caches.