Mr.noob
Mr.noob

Reputation: 1

PHP: problems with upload files while refreshing page

I'm Just working on a simple form in PHP but i have a problem, i have many fields in my form and one of these is a file input type, if there are no issues with the file uploaded but an error occurred on another field, How can I ( in the refreshed page) maintain the file previously uploaded?

Upvotes: 0

Views: 774

Answers (2)

Kelwen Souza
Kelwen Souza

Reputation: 109

Edit: We understood what you want, but this is not possible, filling the file-input again automatically after validation. The user has to select his files again, this is how browsers works, for security matters.

Well, since you know what you're doing, you dont really need a script, you really need some help/ideas on how to approach what you want. What I would do is:

- Save the path of the uploaded file in session user data variable, like $_SESSION['uploaded_file'], and retrieve it after error.
- Use AJAX, different form, to upload the file before user hit submit.

Obs: If the user gives up on completing the form even though he uploaded a file, his files will be on your server, 'stealing' some useful space. So I don't know if this is a really good approach, but a good one, would be using different forms, disassembling the upload action from the form's informations.

Real example: If user is filling out a form to be a member of your website/forum/whatever, just let him upload the profile picture later, only when his account is really active. You could be saving some trouble.

Another one: If the user just wants to change profile picture, also make the uploading action to have no influence (and vice-versa) with his profile informations and etc. This is a trouble-saver, and a better approaching solution.

But, if you're like uploading a photo to a gallery and setting description and etc for the picture, then you could be using of a more elaborated validation, using javascript/ajax to check if fields are being filled correctly before submiting data.

Upvotes: 2

gfcodix
gfcodix

Reputation: 181

On your upload code first save the values into SESSION like:

$_SESSION[name] = $_POST[name];

then print it into your INPUTS like:

<input type="text" name="name" value="<?=$_SESSION[name]?>">

Don't forget to use session_start().

Or look for javascript alternatives

Upvotes: 0

Related Questions