34

Okay so here's the scenario:

User is presented with form that contains file inputs. User submits form. Form does not pass validation/verification for one reason or another. User is presented form with errors highlighted. File inputs are now blank.

Question:

Is it possible to re-populate the file inputs with the paths to the files the user originally selected. A PHP solution would be ideal, but I'm also open to JavaScript solutions as well.

3
  • 6
    This is a duplicate of stackoverflow.com/questions/4205634/… Commented Jul 27, 2011 at 10:06
  • Ah yes... compeletely forgot the security aspect... will close the question. Commented Jul 27, 2011 at 10:26
  • Can people help me close this... it needs more. Commented Jul 27, 2011 at 11:29

2 Answers 2

41

I think the short answer here is no. You can't repopulate file upload fields. However, you can work around it.

If a file has been selected and the form submitted, then you've already received the file. What you can do is keep a reference to the file on disk and put that in a hidden field and show a message to indicate to the user you still have their file uploaded so it does not need to be replaced/re-uploaded. When your form gets submitted again without a file, you can check for the hidden field value and use that to get your local copy of the file they uploaded in their last attempt.

The other way to do this is to either submit the form via ajax (using either flash or the iframe method for the file upload) or to do an ajax call to validate the form first, and then only submit as normal if valid (thus no form reload, no loss of file upload field data).

Sign up to request clarification or add additional context in comments.

Comments

2

I solved this problem by let user pick upload file as the last field before submit the form ^^

2 Comments

That's so smart!
@conradkdotcom ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.