How to keep the selected image if the validation fail in codeigniter

Hi,

I’m happy to be a member of Sitepoint :smile:. I have a form with random inputs such as text and checkbox. at the bottom of my form, I have one input file with the following code:

<input type"file" name="userfile">

now, if the validation fail, I want to keep the selected image and print that there is an image already set. how can I do that? Thanks

Hi everyone, I found solution but i’m not sure if it’s a good solution or not, at least it works.

First, upload the image even if the validation fail, and save the name of image in a session and print it beside the input to show to user that the image is set.

Later, if the validation success, just process the form and delete the session for the image.

Sometimes user might change his/her mind and upload another image instead of the previous one, in this case, just remove the previous one by looking at the name from the session and save the new image’s name in the session to override the name.

I tried this method and works for me. again not sure if this method of good.

Thanks :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.