Retaining input value in a file input form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Retaining input value in a file input form

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Retaining input value in a file input form

Post 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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Retaining input value in a file input form

Post 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.
generationgav
Forum Newbie
Posts: 6
Joined: Fri Mar 28, 2008 6:54 am

Re: Retaining input value in a file input form

Post 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.
Post Reply