Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring the value of a input type=file after failed validation

Tags:

html

file

I have a form with several inputs that deal with files. The javascript validation is pretty good at checking to make sure that everything is good, but some things require PHP to inspect (like the file's mime type), and sometimes it will get rejected.

The problem is, when I send the user back to the form, I can repopulate all the data that they had originally input, except the inputs with the type of file. Firefox doesn't provide the absolute file path so I can't just copy in a file path to the input.

What can I do to repopulate the input type=file form elements?

like image 252
Malfist Avatar asked Nov 17 '10 14:11

Malfist


1 Answers

You cannot populate the file input, to do so would be a serious security problem.

Instead, store the uploaded content on the server and store an id that you can use to reference it in a hidden input.

Clean the files up automatically after they reach a certain age, and provide a means for the user to change their mind about what file they want to upload (e.g. a checkbox (checked by default) for each file being stored on the server for upload)

like image 71
Quentin Avatar answered Sep 22 '22 19:09

Quentin