Reattach a csv file to form after submitting initial form - php

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?

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.

Related

Pass a file through a POST form

I have a page.php where I load a .csv file, read it and eventually upload the values into a database. What I want to do is, once the file is loaded in my form, and I've read it, first I check into my database if I still have space available, and if there is no space left, I print a message saying there is no space left, do you want to upload it anyway? I can get till this point, but I don't know how to send the file that has already been loaded into my form, to the same page through a post form, or any other method.
Here is how I read the file when I upload it
$file = $_FILES['file']['tmp_name'];
but if I try to send $file variable through a hidden field in the new POST form, it doesn't work.
You cannot send a file through hidden inputs due to security reasons. what I would recommend is to save the uploaded file on server when you read it the first time. Then if user clicks on anyway, then pass this saved file as the input for this 2nd iteration.
It can not repopulate field input.
So you can
1) use ajax to check if there are enough space and show message if no space left
or
2) store data from file in file on web server, and store data about it in hidden field in html form and after second submit use stored file.

Html/PHP File Uploader in step 1, but uploading in last step

so basically I have a multistep form with X steps.
In the first step, I offer a file upload form to the user.
This just behaves like a normal HTML file uploader:
User selects file
File Box, shows pathname
If the user clicks on "next" the file should not be uploaded.
If the user reaches the last step of the form, the files he/she has choosen in step-1 should be uploaded now.
I have no idea if this is actually possible. There are also a couple of problems
The value from input type="file" gives something like C:\fakepath\filename.ext
Saving the above in a session for later upload does not make sense.
How can I get the uploader to work in the last step?
Yes, this is actually possible, and saving data between pages make sense, otherwise, how could you remember what he sets in the file input?
If it is a simple HTML page, I assume your are using PHP. Register files in a temporary variable or session, put it in a hidden input in the HTML code if you don't use session.
At the last step, set your files and upload it.
Good luck.

File upload over multiple pages

I'm creating a php form that works in this way:
1) The user fills the form and add a file to upload;
2) The submitted information is shown to the user for confirmation;
3) Upon confirmation, a email is sent with all the data, including the file.
My problem is that I get access to the uploaded file on the second step, but not on the third step. Due to security reasons, it's not possible to resend the file on the second step by creating a input file field with a default value (the uploaded file).
It's possible to achieve what I'm trying to do, without having to copy the uploaded file to another folder?
The solution to my question was to place the actual file upload only on confirmation page. There was no way to "carry" the file upload from a page to another in the way I wanted to.
Thanks for the replies.

Transfering a uploaded file from page to page

My client wants to have a 3 page form. The first page allows the user to enter data including a uploaded file. the second page confirms this data. and the third page submits the data to the database and directories.
Via post, I can keep saving the data to a hidden input fields, thats no problem. My problem is the uploaded file. how do I hold that document from page to page ? I am using Cakephp but any advice would help, thanks
You can always just create the illustion that the form is utilising three different pages. Use AJAX to accept and validate/request the user confirm their submitted data. If in this view they accept it initiate a POST to submit all that data.
You really don't need three physically different files to achieve this but you can still let it appear in three stages to keep your client happy.
You just upload the file to temp directory and keep the value in hidden variables just like other form data . If form successfully submitted then the image copy to desired location other wise delete the image
You can easily fake these 3 pages using CSS. Or even 2, as "third page" is actually a server script which has nothing to do with pages in the browser.
Just make your form, then put an event on the submit button which changes divs to whatever "confirmation page" he wants. and then actually send the form using a button on this page.
that's all
An uploaded file is always held temporarily. The server env var should tell you where it is. In Ruby's rack it is stored in the params var. So I guess there is a similar params var in php which has a hash with all the necessary information.
Since the file would be uploaded on the first step, one option is to put the file's location in a hidden input field along with the rest of the data (either there, or put it in the session). With CakePHP, if your file field looks somewhat like that:
<input type="file" name="data[User][image]" id="UserImage" />
Then you will be able to capture the location through
$location = $this->data['User']['image']['tmp_name'];
Which will correspond to something like /var/tmp/xxxxxx
On the last page, if the user confirms all the data, you just use move_uploaded_file() to put the file wherever you want on the server.
move_uploaded_file($location, '/new/location');

Proper way to handle uploads using AJAX form

I'm using Valum's AJAX uploader, which is quite nice. I have a form that lets you fill out some info and optionally attach files.
I have a hidden input on the form that has a randomly generated "token" (5 character alnum). The uploads are sent to a tmp folder and the info about those files (name, dir, token) are kept in a uploads_tmp table in a database.
Then, when the user successfully submits the form, those files are moved to a more permanent location and the rows from the uploads_tmp are moved to the uploads table.
If the user submits and there are errors with the form, my script knows there are uploads from a previous attempt via the token. So there's no need to re-upload files.
Is this the right way of doing it or am I going about it all wrong? I'm using PHP (CodeIgniter to be exact).
There isn't any one correct way of doing something like this. Your method seems like a good intuitive one, but really it's down to whatever works for you and your situation.

Categories