How to pass a file through two forms - php

I have the following form that allows uploading a file:
<form id="form1" name="form1" method="post" action="page1.php" enctype="multipart/form-data">>
CSV list: <input type="file" name="file" id="file">
<input type="submit">
</form>
When I go to my action/submit page I can retrieve the file and the temp location of the file, but I need to send it through again to a new page, so I do the following:
<form method="post" action="page2.php" name="details" >
<input type="hidden" id="type" name="type" value="1">
<input type="hidden" id="filename" name="filename" value="<? echo $_FILES["file"]["tmp_name"]; ?>">
<input type="submit" value="Insert Into Database">
</form>
This doesn't work because the temp file is already gone.... how can I pass the file through?

I think you want to rethink how you are getting this data. If it was me, I would want to have the first form upload the file to a temporary directory and pass the path to the file through a $_REQUEST. Then use a hidden input field pass the file path name through PHP and if they confirm their selection on the second form, do whatever you want with the file using the file's path. This means you don't have to upload two files(not efficient) and you can pass the data onto the second form.

Related

how to upload multiple text files and save path to MySQL database using PHP?

i am looking for a code that how to upload multiple text files to be input in different input fields like
<input type="file" name="file">
<input type="file" name="file2">
these files should be text files,upload to the specified directory and save this path to MySQL database later get the file paths for specified user only
You can make it in array as below.
<input type="file" name="file[]">
<input type="file" name="file[]">
Make sure to use enctype="multipart/form-data" in form tag.
<form action="http://localhost:8000" method="post" enctype="multipart/form-data">
<input type="file" name="file[]">
<input type="file" name="file[]">
</form>
And if you want to select multiple files with single file input then add multiple="multiple"
<input name="file[]" type="file" multiple="multiple" />

HTML form - keep reference to uploaded file for 2 button options

I have an html form and 2 php files, all of which work well. In the form I created a preview button that references a php file that shows the uploaded cvs in an html table as a preview. The submit button inserts it into a database.
I would like to have the form contain one button that goes to the preview page and once the info is confirmed, there would be another button that references the submitted file, but I can't seem to get that to work.
Due to the environment I'm trying to get this to work without javascript or ajax if possible, as another user gave me a javascript option.
Here is the html form:
<form method="post" action="confirm.php" enctype="multipart/form-data">
<label>Upload File</label>
<input type="file" name="file">
<input type="submit" name="submit" value="Confirm" >
</form>
<form method="post" action="upload.php" enctype="multipart/form-data">
<label>Upload File</label>
<input type="file" name="file">
<input type="submit" name="submit" value="Submit">
</form>
Each Php file corresponds as well:
if(isset($_POST['submit']))
Is there any way to go from upload to preview to submit and keep reference to $file?

PHP Multiple File upload - some form variables omitted after selecting too many (large?) files?

I have a very basic PHP multiple file upload form:
<form action="file.upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $_REQUEST['id']; ?>" />
<input type="hidden" name="bypass" value="1" />
<input multiple type="file" name="file[]" id="file">
<input type="submit" name="submit" value="Submit">
</form>
Everything works fine for a few files, but when I upload too many large files, the hidden variables are no longer posted to PHP (!)
I am not sure what is causing the from variables to be ignored by PHP (?)
(I've checked with fiddler and the form indeed sends the right data from clientside... i.e., form fields bypass and id are both properly populated on clientside...)

PHP session upload progress and simultaneous uploads

The PHP documentation doesn't explain what happens when there are two files being uploaded at the same time within the same session (two uploads running in two tabs of a browser).
Is there any way to track the progress of both uploads?
Is the first upload status lost when the second one starts?
Thanks!
Yes, it is possible to monitor the status on two different uploads in different tabs using PHP's Session Upload Progress feature. All you need to do is make the upload progress name different on both forms by changing the value="" parameter of the hidden upload progress name field.
For example, the upload form for tab 1 could look as follows:
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="tab1">
<!-- notice the value="tab1" above -->
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="myUploadName" />
<input type="submit" />
</form>
Then, the upload form for tab 2 could look as follows:
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="tab2">
<!-- notice the value="tab2" above -->
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="myUploadName" />
<input type="submit" />
</form>
Now that you have created two different upload progress sessions, you can get the progress data on the PHP side as follows:
$_SESSION['upload_progress_tab1'] // Progress data for tab 1
$_SESSION['upload_progress_tab2'] // Progress data for tab 2
This example could work for you:
http://www.johnboy.com/php-upload-progress-bar/
You need some client (JS) code and a bit of PHP

How to retain the input file type post data

How can I redirect a page with a file POST data on it? Something like
Page1.php
<html>
<form action="page2.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Send"/>
</form>
</html>
Page2.php
<html>
<form action="page3.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/> ### get the value of file from page1.php and send to page 3
<input type="submit" value="Send"/>
</form>
</html>
Is it possible?
Not that way... the file is uploaded when the user submits the form. You could then base64 encode that file and embed it in the HTML as a hidden field to be submitted on the second form, but that's very inefficient.
You should handle the file upload on the first submission. Save the file somewhere or stick it in your database, whichever works better for the files you're receiving. If you save the file, I'd recommend generating a unique name for it (like a UUID). Then use the filename or the primary key from the DB as a value in a hidden field in the second form, which you can use to find the file you already received when the user submits the second form.

Categories