Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have added a source code content type with filefield to upload user's file in node. However, when they edit their own content and upload file with the same filename the old one is not overwritten. How do I overwrite the old one with the new file?
Thanks
Comments
Comment #1
quicksketchYou can't directly "overwrite" an existing file, because FileField doesn't know if you're going to save the changes or not. Say you have the following workflow:
- Create a new node and upload example.png
- Edit that same node and upload example.png again, which is renamed to example_0.png automatically. But then don't save the node.
- You leave the page and the post is never saved.
So this situation, it shows why FileField doesn't replace the existing file. If the node is never saved, the original file obviously shouldn't be removed. Also if the user uploads a file, they might want to include that file's URL (which is example_0.png) in the body of the post. If we moved the uploaded file after the node was saved, then the URL within the body would break.
Comment #2
jaypabs CreditAttribution: jaypabs commentedI hope that the way filefield will handle changes will do like this:
If preview then do not upload or overwrite file, since this is not yet the final action. It is just a preview of the node.
If save then overwrite file.
Comment #3
quicksketchThis won't work for the reason I stated above:
Comment #4
jaypabs CreditAttribution: jaypabs commentedIf the user will use different filename then delete the old one. This will work only on single file upload. If you have setup a multiple upload using file field then do not delete the old file. Instead just append the new one.
Comment #5
quicksketchThe old file is deleted if it is no longer used. But like I said, we can't rename the new file after it's been uploaded, because the user might use the new file's URL within the post.
Comment #6
jaypabs CreditAttribution: jaypabs commentedI have seen so many old file which is not deleted after uploading a new file in my server.
Did you update this? On what version? Because I was using filefield since version 2.0 I think, if not version 1.0.
And for the filename again, if the user upload the same filename then filefield should overwrite the old file, and don't rename it something like filename_0.zip. And if you are worrying about referencing a filename in the textfield, then if the user upload with the same filename, then make a reference on that same filename.
Comment #7
quicksketchThey're always deleted eventually. See #419180: Delete Temporary Files Sooner. Note that when updating or editing a node, the files are deleted immediately if they're no longer needed. It's only when you upload a new files but remove them before saving that the files are not deleted immediately.
No it shouldn't. See all the reasons I've stated above.
Comment #8
quicksketch#1 explains why we can't do this as the default behavior. A work-around if you absolutely need the same file name: Edit the node, remove the file and save. Edit the node again and upload the new file.
I'm marking this fixed as a support request, since there's nothing else FileField can provide.
Comment #9
jaypabs CreditAttribution: jaypabs commentedIt will, for those who know this process. I have a lot of members, who do not know about this. In users perspective, all they do is change it without doing the process above, as you mentioned.
Comment #11
markDrupal CreditAttribution: markDrupal commentedUPDATE:I moved this to its own module here: http://drupal.org/project/upload_replace
I ran in the same problem as #9. My users cannot be expected to be retrained to use this module by using a work around of delete-save-edit-add
I wrote my own module for fixing this for myself, if anyone else has similar issues, i'd appreciate some feed back or validation that this works for you.
This module notices when a file has been renamed by adding a "_1", "_2" etc and updates the file name by renaming it from
<filename>_1.<ext> to <filenamde>.<ext>
It also finds the file that was blocking this action from occurring and swaps names so the newest file will retain its file name and the older file gets the "_1".
Tested with the file field CCK and works with node versioning and reverting old version of a node works as well. When reverting, the reverted node always keeps the correct file name.
Thanks
here are the 2 files: upload_replace.info and upload_replace.module
Comment #12
markDrupal CreditAttribution: markDrupal commentedI moved this to its own module here:
http://drupal.org/project/upload_replace
Comment #13
jaypabs CreditAttribution: jaypabs commentedThat was great mark. You have a good idea here. Rename the old file as _1 and retain the new filename.
Comment #14
Berliner-dupe CreditAttribution: Berliner-dupe commentedThank you Mark for this helpful module - it works great. I am so happy ;-)
Comment #15
rituraj.gupta CreditAttribution: rituraj.gupta commentedhook_file_update is not calling on update the image in Drupal 7. What is the reason ?
Comment #16
ravyg CreditAttribution: ravyg commentedI think in code if you are using "FILE_EXISTS_REPLACE" should work not 100% sure.
For Reference:
file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
FILE_EXISTS_REPLACE - Replace the existing file. If a managed file with the destination name exists then its database entry will be updated. If no database entry is found then a new one will be created.
FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique.
FILE_EXISTS_ERROR - Do nothing and return FALSE.
Comment #17
rituraj.gupta CreditAttribution: rituraj.gupta commented@ravg, FILE_EXISTS_REPLACE always work and 100 % sure for this. Please check if parameters are being send correctly and permissions are given correctly to the folders.
Comment #18
johnnny83 CreditAttribution: johnnny83 commentedI don't understand why after seven years this is still not in core...
Comment #19
NicholasSThe original solution for this should have been to just ask the user if they would like to overwrite the file when they select the new file to upload or rename it. DONE! Ignore the edge case of someone 'might' not wanting to save the node. Given that didn't happen www.drupal.org/project/upload_replace is the best solution I guess.
Comment #20
johnnny83 CreditAttribution: johnnny83 commentedYeah, I know this module, but unfortunately no D8 port available. Seems like I still have to use FTP :D.
Comment #21
kevinquillen CreditAttribution: kevinquillen at Velir commentedFTP doesn't solve this, because it doesn't account for the field data you need to relate fields to entity content and vice versa.
I, for one, would love to have the option when creating a field what the default behavior should be (REPLACE vs RENAME, etc).