3

Note:

The answers & comments below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element dynamically/programatically using JavaScript in 2017.

See the answer in this question for details as well as a demo:
How to set file input value programatically (i.e.: when drag-dropping files)?

How can I setup an <input> tag where when the user submits the form, it will still show the previously selected file?

I've tried the following:

<input type="file" name="upfile" value="<?=isset($_POST['upfile'])?$_POST['upfile']:''?>"/>

but it doesn't seem to work!

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
  • Looks ok but I'm not sure about this functionality with file...`$_POST['upfile']` is a file, right? So what do you want the input to show, the file name? – Dan Sep 18 '14 at 22:05
  • This has been asked several times in somewhat different formulations. See e.g. http://stackoverflow.com/questions/4205634/restoring-the-value-of-a-input-type-file-after-failed-validation – Jukka K. Korpela Sep 19 '14 at 04:34

2 Answers2

3

With input type file it isn't possible to set up a default value. You have to use some plugins e.g. http://blueimp.github.io/jQuery-File-Upload/

Read following article How to set a value to a file input in HTML?

Community
  • 1
  • 1
algorhythm
  • 8,530
  • 3
  • 35
  • 47
3

That's impossible to set a selected file by default. It against the security policy. If you want to keep previously selected file, you should not reload the page. Submit your form using ajax and you will keep your file selected so you can submit it again or do whatever you want

Tengiz
  • 1,902
  • 14
  • 12