php - Fill file input after form submit / on form submit error -



php - Fill file input after form submit / on form submit error -

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

i´m using combination of form validation class , upload library of codeigniter form submission. works great.

i´ve 1 problem haven´t found solution yet: if user selects image misses fill required field (like "name"), form validation class blocks request , shows error message customer.

but i´ve problem, image submitted , don´t want allow user add together file again. want pre-fill file input data.

i´ve tried different things like:

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

and spent time on finding solution on web without success.

on server side, not info where file located on client's computer, in scenario of user uploading image user hasn't filled out rest of fields properly, have omit input type="file" field exclusively maintain store of file located on server. there's few ways go this, involves taking absolute location of uploaded file and:

inserting hidden value using <input type="hidden" name="uploadedfile" value="<?php echo $abspath; ?>" /> checking existence of $_post['uploadedfile'] , utilizing appropriately. isn't solid thought you're exposing server paths end-user (opens malicious attack.) starting session , saving absolute path in $_session variable while presenting user simple token in re-attempt form.

i'd stick method 2, assuming you've done work validate form , upload file , file located in $absfilepath, following:

session_start(); // needs @ top of php file // ... $formtoken = md5(time()); $_session['uploadedfile'][$formtoken] = $absfilepath;

then render token hidden variable using:

if (!empty($_session['uploadedfile'][$formtoken])) echo '<input type="hidden" name="formtoken" value="'.$formtoken.'" />';

and hide file upload portion using

if (empty($_session['uploadedfile'][$formtoken])) echo // <input type="file" output here...

finally within of form submission code check existence of formtoken value before attempting load $_files['image'] using isset($_post['formtoken']), , handle using:

$absfilepath = $_session['uploadedfile'][$_post['formtoken']];

bam! have absolute file path if file had been uploaded before.

since haven't given plenty code, can given plenty instruction started, should more enough.

php forms codeigniter

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -