Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill file input after form submit / on form submit error

I´ve a multipart form with a mixture of default inputs (text, select etc.) and a file upload (<input type="file">).

I´m using a combination of form validation class and upload library of Codeigniter for form submission. That works great.

I´ve only one problem for what I haven´t found a solution yet: If the user selects an image but misses to fill another required field (like "name"), then the form validation class blocks the request and shows an error message to the customer.

But now I´ve the problem, that the image was already submitted successfully and I don´t want to let the user add the file again. So I want to pre-fill the file input with this data.

I´ve tried different things like:

<input type="file" name="image" value="<?php echo set_value('image','');?>" />

and also spent time on finding a solution on the web but without success.

like image 563
Michael Avatar asked Mar 18 '23 23:03

Michael


1 Answers

On the server side, you do not get any information about where the file is located on the client's computer, so in the scenario of a user uploading an image successfully but the user hasn't filled out the rest of the fields properly, you have to simply omit the input type="file" field entirely but keep a store of where the file is located on your server. There's a few ways to go about this, but it all involves taking the absolute location of the uploaded file and:

  1. Inserting it back as a hidden value using <input type="hidden" name="uploadedFile" value="<?php echo $absPath; ?>" /> then checking for the existence of $_POST['uploadedFile'] and utilizing it appropriately. But this isn't a solid idea as you're now exposing server paths to the end-user (opens yourself up to malicious attack.)
  2. Starting a session and saving the absolute path in the $_SESSION variable while presenting the user with a simple token in their re-attempt form.

I'd stick with method 2, so assuming you've done all the work to validate the form and upload the file and your file is located in $absFilePath, you could do the following:

session_start(); // This needs to be at the very top of you PHP file

// ...

$formToken = md5(time());
$_SESSION['uploadedFile'][$formToken] = $absFilePath;

Then render the token as a hidden variable using:

if (!empty($_SESSION['uploadedFile'][$formToken]))
    echo '<input type="hidden" name="formToken" value="'.$formToken.'" />';

and hide the file upload portion using

if (empty($_SESSION['uploadedFile'][$formToken]))
    echo // <input type="file" output here...

finally inside of your form submission code check for the existence of a formToken value before attempting to load $_FILES['image'] using isset($_POST['formToken']), and handle it using:

$absFilePath = $_SESSION['uploadedFile'][$_POST['formToken']];

Bam! Now you have your absolute file path as if the file had been uploaded just like before.

Since you haven't given enough code, I can only given you enough instruction to get you started, but this should be more than enough.

like image 111
sjagr Avatar answered Mar 26 '23 03:03

sjagr