Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retaining the value in the <input type="file"> field when submit button is clicked

Tags:

html

php

I have got a <input type="file" > field along with other text fields in a form, When i try to upload any files using browse button and then click submit button ,the value in the input type= "file" field disappears , I would like the browsed value to remain in the <input type="file" > field if errors are present in other fields , is there any way i can retain the value that is browsed and for it to remain in the <input type="file" > field when submit button is clicked ,

<form action="form.php" method="post" enctype= multipart/form-data> 

<input type="file" value="$file" name="file"/> 
<input type="text" value="$line" name="line"> 
 <input type="submit"  name="btnsubmit"> 

</form> 

if($_POST['btnsubmit']) 
{
$line =$_POST['line'];
$file =$_FILES['file'] ['name'];

if($line) 
{
//do something
//conditions for   file check   here 

}
else 
//error 

}
like image 215
Abida Avatar asked Jul 11 '12 06:07

Abida


People also ask

Why does set_value () not work with an input file type tag?

The function set_value () will not work with an input file type tag because of the HTML specification and because browsers do not allow to preset a value for this type of input field. does not work. The value attribute is not accepted.

Why we can’t take file input by default in HTML?

But when we want to take file input by default, then we cannot do it. It means we cannot set a value to a file input due to some security reasons in HTML. the value attribute.

How to take a file as an input in a form?

In HTML, we will use the type attribute to take input in a form and when we have to take the file as an input, the file value of the type attribute allows us to define an element for the file uploads.

How to resubmit a file input box with a label?

I would suggest coupling the file input box with a label or text box. Then you can populate the it with the value from the file input box to be resubmitted later. Show activity on this post.


2 Answers

It is not possible to do this. Browser security prevents you from pre-populating the File input field, so that websites cannot steal private files of their will without the user authorizing it first (by clicking "Browse..." and choosing a file).

EDIT: It is not possible to do this natively - but you can attempt some CSS wizardry to display the previously chosen file name maybe beside the file input box to hint the user. If you want to try and be really cool, you can also attempt to overlay the browser's native file input text display area with another div that has the previous file name filled in it. But this will prevent clicking on the input area and so is user unfriendly. Too much work, little reward.

like image 117
Aditya M P Avatar answered Sep 21 '22 09:09

Aditya M P


This not allowed to be set by any script for security purpose, implemented by browser vendors as file input as readonly.

like image 42
Raab Avatar answered Sep 22 '22 09:09

Raab