Tutorial :Form submitting multiple files not retaining data



Question:

Ok so I have a HTML form, 10 files get uploaded. 6 images and 4 pdf. some of theses files are required others not.

My problem is when my script verifys the data and retuns to the user that he/she has errors all the file inputs are come back as blank... and if the user re-submits... the files are not saved.

This form has other fields other than the files as well.

I tried putting $_POST in the value of my inputs but that does not repopulate with the local file names.

I would like it so my users dont need to re enter all the files they want to upload if they made 1 or a few mistakes.

edited: SO is this normal that the post isnt giving me the file name? is the file still saved as temp on the server?


Solution:1

The value attribute of the file input type doesn't exist as this is a security risk. You can't read it or set the value of it (client or server side) so even if you had the local path on the user's computer you couldn't fill it in.

I would try to do some sort of client side (even through ajax) form validation before they submit the form. If they do submit the form, the files will upload and should still be in the temp directory (at least until garbage collection kicks in). Maybe on error write the information in $_FILES to session and instead of showing the file upload dialog, show an icon for the file to try to let them know you got the file, but they need to fix the form for everything to finish. Then after they submit the corrections you can get the data from the session and try to continue.


Solution:2

Make sure to specify proper settings for:

  • file_uploads
  • upload_max_filesize
  • memory_limit
  • max_execution_time
  • post_max_size

See:

Also make sure that:

  • You have specified the enctype="multipart" in the form
  • Check the files array with print_r($_FILES);


Solution:3

Instead of redirecting them back to form use a bit of JavaScript and make client browser to go one page back. Then he should file that were selected before.
You could use something like:

<body onLoad="javascript:history.go(-1)">...</body>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »