0

i have this scenario:

The user enters the web, complete some standard text inputs and select a file from an "input type=file".
When the OK button is clicked i run some ajax checks and if success the form is submitted and show some other divs with parsed data and a "confirm action" button.
I want to upload the file to the server with php if the user clicks the "confirm action".
I honestly have no good ideas on how to do that... maybe save the $_FILES into a session but i read its a really bad idea.

What do you recommend?

Emepese
  • 419
  • 3
  • 15
  • See https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload for how to upload a file with AJAX. Run the code in the callback of the first AJAX. – Barmar Sep 02 '17 at 01:35
  • You can't save `$_FILES` in a session variable. The temporary file for the upload is deleted automatically when the script ends, so the file has to be saved in the script that processes `$_FILES`. – Barmar Sep 02 '17 at 01:36
  • @Barmar but i submit the first form where the file is, so i lose it from the ajax/jquery scope? Or there is a way to keep that? – Emepese Sep 02 '17 at 01:45
  • I thought you're sending AJAX, not submitting the form. – Barmar Sep 02 '17 at 01:51
  • i send a first ajax before submitting the form, after that first ajax i need to send a second one and upload the file for the first form. – Emepese Sep 02 '17 at 02:02
  • From your explanation above, it seems you never reload the page before wanting to upload the file, so it should already be there, just submit the form – adeneo Sep 02 '17 at 02:05
  • I explain that badly. After the first ajax y submit the form. – Emepese Sep 02 '17 at 02:11
  • @Emepese If you do that, then the file should get uploaded in the form submission. – Barmar Sep 02 '17 at 02:15
  • @barmar but if the process goes wrong or the user doesnt confirm the operation the file is already uploaded to the server. I suppose i can upload to a tmp folder and then move it when the operation gets confirmed. But i wanted to see if theres an option to avoid that so i dont have to run a cronjob to remove those files. – Emepese Sep 02 '17 at 02:21
  • If the user doesn't confirm, then you don't call `form.submit()` to submit the form. – Barmar Sep 02 '17 at 04:18

0 Answers0