Prevent HTML file field from being delete after PHP validation roundtrip [duplicate] - php

This question already has answers here:
Can you re-populate file inputs after failed form submission with PHP or JavaScript?
(2 answers)
Closed 10 years ago.
I have a HTML file field <input name="File" id="FileName" type="file"/>. When I submit the form, I do receive the file, all fine. However, when I validate the user's input and cannot accept the values provided, I will need another round trip. As usual, I will redisplay the form plus error messages.
For text field or textarea I can set the (already) provided value, e.g. by value or just echo the value in the textarea. How could I do the same for file fields? I want to avoid that the user always has to reselect the file in the browser.
-- added --
Comments below are right - there are workarounds described in the mentioned SO questions:
PHP remember file field contents
Can you re-populate file inputs after failed form submission with PHP or JavaScript?
Restoring the value of a input type=file after failed validation

This is not possible - you have to store the file on the server to do this. Then you can show the user a short info about the already stored file and the option to upload a new file or delete the file

Related

When using <input type="file"> is there any way to populate the input box with some default value? [duplicate]

This question already has an answer here:
How to set file input value when dropping file on page? [duplicate]
(1 answer)
Closed 2 years ago.
I was using PHP for updating data in Mysql and when I open the update link then if I don't input anything in the file input type then after clicking on the submit button the previously present file/image is not sent.image of update link
But when I input something in input file box in update page then image is uploaded.
Short answer: no.
Longer version: theres no way for you to set a default value for a file-selection. because a file-input points to a file on the local computer (if one was selected). it would be a great security issue if a web dev could f.e. say "make the local passwords file the default value".
you have to handle this another way - default value is not an option for file inputs :)
best,
stefan

How to restore input file type field after failling form validation in cakephp [duplicate]

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.
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).
I solved this problem by let user pick upload file as the last field before submit the form ^^

Don't lose input type="file" value after page reload [duplicate]

This question already has an answer here:
Restoring the value of a input type=file after failed validation
(1 answer)
Closed 8 years ago.
I have a problem with an form on my website. The form contains some text inputs and a file upload input. When some text fields aren't filled correctly, there come's an error and the page is reloaded. But: If I choose a file, don't fill out the other files correctly and the page is reloaded, I must choose my file again, it isn't saved in the form.
Can anybody help me please?
For security reasons, you cannot set the value of a file input at either client or server.
I can think of these possible work-arounds:
This post provides one method of a server-side solution involving putting an ID in the page that refers to the previously uploaded files rather than the actual file inputs themselves (since the files are likely already uploaded).
Many sites that accept file uploads do them in a somewhat separate form. The file uploads are completed and then the rest of the form is filled out and submitted with the server keeping a reference connection between the previously uploaded files and the current action that will be submitted to the server. The popular forum software vBulletin works that way for message attachments. Attachments are uploaded in a separate form/UI.
You can submit your form via ajax, rather than as a form submission. Because the ajax call does not change the current page, if you get errors from the submission via ajax, then the current page and the selected file inputs are still there. You can add your error reports to the current page with javascript and show the users the error that way.
You can also avoid most of this issue (thought not all), by doing as much client-side validation as possible with javascript before submitting your form so you lessen the possibilities of the server ever rejecting the form submission.

How to get the data from input without attribute name? [duplicate]

This question already has answers here:
Does form data still transfer if the input tag has no name?
(3 answers)
Closed 9 years ago.
I read $_POST is empty after form submission topic and I wondered how to get the data from input without attribute name?
<input type="text" >
var_dump($_REQUEST)//empty
var_dump(file_get_contents('php://input')) //empty
Basically, the form should be specified its name. If not, you cannot get it from the server-side.
However, the form without name is also valid in HTML5. The reason is that you might sometimes want to use for form submission via AJAX or Javascript app. In many cases

How to set the value of a HTML file field? [duplicate]

This question already has answers here:
How to set a value to a file input in HTML?
(10 answers)
Closed 6 years ago.
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 ?
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.
You cant set value of file field by any means.
See here: info
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.

Categories