Avoid overwrite of uploaded files


Imagine a form with a file field. It invites the user to uploade, say, their CV. A first user have a file named cv.pdf. A second user have one file named the same way. If both user apply, it seems that the first user’s CV will be overwritten.

How to avoid this? I would ideally like to suffix the file names, or if not possible to upload to a different subfolder. For that second option, can I do something like destination: /user/data/cvs/{{ form.value.name|e }}maybe?

Thank you :slight_smile:

Mmmm this is something the Form plugin does not handle, going to fix this. It should never overwrite a file, but instead append a suffix.

Also, the destination does not parse twig. Going to make this possible too.

Implemented the destination twig parsing in https://github.com/getgrav/grav-plugin-form/commit/bfffed398bd2be931cb2e0dcd0786ffedfb4c829

Made sure files are not overwritten in https://github.com/getgrav/grav-plugin-form/commit/ea98350d42c53b73d145c793f519ad89f9b0d1bf

Great, thanks for the quick updates!
So can I update my version of the Form plug-in with the one on Github? Or could that cause issues, with the auto-updater or something else?

Yes you can do that, download the zip from Github. Once the update is out, it will be overwritten when you update.

Thanks. I now get an error each time I want to submit the form :

“Validation failed: Missing required field:” [Name of the file field]

I get this whether the selected file has an existing name or a new one.

Actually, the update also broke another form without a file field. The Missing required field in that case is the ReCaptcha field (the last one before the Submit button).

Mmm cannot replicate. Are you using Grav 1.1? I am testing on that. Or 1.0? Care to open an issue on the Form plugin repo https://github.com/getgrav/grav-plugin-form so we can better track this?

I am using 1.0.10. So I reverted back to the 1.1.2 version of the Form plugin because the site goes into production this week. I simply added your latest commit to the form.php file:

if (file_exists("$destination/$name")) {
   $name = date('YmdHis') . '-' . $name;

Now it works perfectly!
And I’m reporting the issue :slight_smile: