0

By default in Drupal 7, if I upload image.jpg via a file field in my node, save the node and then edit that same node and re-upload image.jpg, it will name it image_0.jpg.

I want to replace the image uploaded so I will only ever have image.jpg. How can I do this?

3

As a technical exercise...programatically speaking, you would need to replace the element's #value_callback, currently file_managed_file_value(), with a custom one.

Your custom callback would invoke another custom function, a rewritten version of file_managed_file_save_upload(), which would then invoke file_save_upload() with FILE_EXISTS_REPLACE as the 4th parameter, as opposed to the current value of FILE_EXISTS_RENAME.

Real-world, you don't want to do this. As soon as someone uploads an image to a different node, it will obviously overwrite the existing one called image.jpg, and that new image will be used in place of the existing one for all other nodes.

That's why the renaming functionality exists in the first place; to avoid global collisions.

  • 1
    I just want to add that in IMCE module, there's a setting: "Default behaviour for existing files during file uploads" which has an option: "Replace the existing file with the new one". This would be a good starting point to learn all the nuances of doing this. – Beebee Jan 13 '15 at 14:21
  • Perfect! I was able to do exactly what Clive recommended and it worked. Thank you – Matt Jan 13 '15 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.