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)?

In HTML we can set the value of a textfield by writing :

<input type="text" name="doc_intitule" maxlength="100" size="24" value="<?php echo $ret['doc_intitule']; ?>">

But how to set the value of a file field ?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
pheromix
  • 18,213
  • 29
  • 88
  • 158
  • What's the exact use case of this? Do you plan to display a previously uploaded file or do you want to set some kind of default? – flooooo May 09 '12 at 09:25
  • see http://stackoverflow.com/questions/1326683/php-remember-file-field-contents – Haim Evgi May 09 '12 at 09:26
  • why you 'd like to do that? what is the business case? The file control is used to enable the user to upload a file only. Instead, do you want the user to download a file ? – Ahmed Hashem May 09 '12 at 09:26
  • There is no duplicate : I didn't post this thread to other tags ! – pheromix May 09 '12 at 09:37
  • 1
    Duplication has nothing to do with tags: it has to do with asking a question that has been asked before. – Nanne May 09 '12 at 09:50

3 Answers3

5

Please refer to this

How to keep input type=file field value after failed validation in ASP.NET MVC?

As far as I am aware of, you are not meant to pre-set file field values for security purposes.

Community
  • 1
  • 1
He Hui
  • 2,196
  • 4
  • 29
  • 46
3

You cant set value of file field by any means. See here: info

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
3

You can't set a default value to a file field.

This is a security thing: you can just make a form submit by code, so you would be able to force a user to upload a file without their knowledge.

Nanne
  • 64,065
  • 16
  • 119
  • 163