Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Avoid overwrite of uploaded files

Started by Muut Archive 10 years ago · 9 replies · 431 views
10 years ago

Hello,

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 :)

10 years ago

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.

10 years ago

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?

10 years ago

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

10 years ago

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

TXT
“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.

10 years ago

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).

10 years ago

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:

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

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1370 9 years ago
Archive · by Muut Archive, 9 years ago
2 943 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1127 9 years ago