Page 1 of 1
Retaining input value in a file input form
Posted: Fri Mar 28, 2008 6:26 am
by lettie_dude
Hi
Trying to find out if this is possible.
Have a form that submits several files and the backend code validates and checks them. What I'd like to do is if there is an error in one or more of the 5 file upload checks the other file input forms will remain populated with the previously browsed to file path. I can code for this with any other input field but the files field is illuding me.
Cheers
Re: Retaining input value in a file input form
Posted: Fri Mar 28, 2008 7:27 am
by aceconcepts
I was actually about to do this myself but haven't got round to it yet.
Remember when you submit an uploaded file it is declared as: $_FILES['filename']['name'] - but his just takes the file name not the local path!
Re: Retaining input value in a file input form
Posted: Fri Mar 28, 2008 7:41 am
by onion2k
It's not possible because it'd be a massive security risk (imagine being about to tell a page to upload any file from a users PC.. eek). You wouldn't want to do it even if it was possible. If you populated the file input field then the user would still have to upload the file again ... that'd take ages.
The solution is to temporarily cache the file. When the files are uploaded check them, validate them, and any that are OK move into a temporary holding directory and put the details of it (filename etc) into a session variable. Then when you display the form to the user again asking them to fix whatever the problem was you can add something like "File 1: Ok. File is filename.ext. To change this file select a new one, or leave blank to keep." below the file input box. When the user submits the form check the $_FILES array ... if they selected a new file use that, if they didn't use the one that you moved into the temp directory.
Easy.
Re: Retaining input value in a file input form
Posted: Fri Mar 28, 2008 8:00 am
by generationgav
What I do is to get the user to upload the file in a popup window...so have something like
File: NO FILE [Upload file]
where clicking [Upload file] opens a popup window with the file dialogue box. The file us uploaded and JS is used to swap it to...
File: filename.ext [Remove]
Only downside is JS is required. If you like this option then let me know and I can post some code snippits you might need up here.