0

I have a form that is just a file input, this file input is meant to attach CSV files to, and then you submit it and it gives you a list of all the headers to chose from. Once you are done making selections, I want to submit another form with these choices along with the CSV file to process.

My problem is that I can't figure out how to reattach the CSV file to the 2nd form.

I thought about just using an array to just POST the data, but wouldn't it be better to just reattach the CSV file and handle it properly?

This is what I have, I tried to look this up but couldn't find anything.

<input type="file" name="fileToUpload" value="<?=$_FILES['fileToUpload']['tmp_name'];?>">

That is in the 2nd form, after the initial attachment. How can I attach the CSV file to the form to process?

GrumpyCrouton
  • 7,816
  • 5
  • 27
  • 61
  • you could probably pull this off with an ajax call, and won't need a second form, just call another php file to process it – Funk Forty Niner May 04 '17 at 12:51
  • So how could I pass the data to that php file? Just POST it as an array? – GrumpyCrouton May 04 '17 at 12:52
  • you could post it as an array or you can use the javascript formdata object to submit files over ajax http://stackoverflow.com/questions/21044798/how-to-use-formdata-for-ajax-file-upload – M31 May 04 '17 at 12:53
  • @GrumpyCrouton This Q&A http://stackoverflow.com/q/16638181/1415724 could help and http://stackoverflow.com/q/19182199/1415724 - I found those when Googling "process multiple forms php ajax" if you wish to further your search. – Funk Forty Niner May 04 '17 at 12:55
  • I'm still not really sure how I could use that, because there is user input required after the first form, so then I'm back to my initial issue as to not knowing how to reattach the file – GrumpyCrouton May 04 '17 at 12:58

1 Answers1

1

What you're describing is impossible - you can't use PHP to write a file into a "file" input - it represents a location from the user's disk (not known to you on the server side) and only retrieves the actual file contents at postback.

Instead I suggest that after the first form submission, you store the file on the server's disk in a temp location, and store the location in the session (or DB, or any other persistent storage), then on the 2nd postback retrieve the location and continue using the file. That way you don't have to request it again.

Once you're done with the file you can delete the temporary file, or if that's not practical within the lifetime of the request, have a secondary job that clears the temp files folder of all "processed" files (and any files where the user never submitted the second form and their session has ended) at regular intervals.

ADyson
  • 44,946
  • 12
  • 41
  • 55
  • It's the only feasible approach as far as I can see. Also, let's say your suggestion was actually viable, this would save bandwidth as well because the file is uploaded once, instead of being uploaded-downloaded-uploaded again. – ADyson May 04 '17 at 13:11
  • Well the file sizes that I'm working with are pretty small in most cases but you are right – GrumpyCrouton May 04 '17 at 13:12