| | |
File Upload Variables
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
I am working on a 2 page form. I need to have a file upload on the first page. I need to pass the file upload values to the insert page. I am not sure what approach to take. I have tried sessions but had no luck.
Here is a simplified version of my code.
Page 1:
Page 2:
Page 3:
Need to get the upload values from Page1 - Page3
Thank you in advance,
Scott
Here is a simplified version of my code.
Page 1:
php Syntax (Toggle Plain Text)
<form method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> <input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </form>
Page 2:
php Syntax (Toggle Plain Text)
<form action="insert.php" method="post"> <input type="text" name="first" id="first" /> <input type="submit" name="button" id="button" value="Submit" /> </form>
Page 3:
php Syntax (Toggle Plain Text)
<?php $uploadDir = 'upload/'; if(isset($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } include 'includes/config.php'; include 'includes/opendb.php'; if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $query = "INSERT INTO contacts (first, name, size, type, path ) ". "VALUES ('$first', '$fileName', '$fileSize', '$fileType', '$filePath')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); include 'includes/closedb.php'; echo "<br>Files uploaded<br>"; } ?>
Need to get the upload values from Page1 - Page3
Thank you in advance,
Scott
Last edited by peter_budo; May 11th, 2008 at 1:58 pm. Reason: Keep It Organized - please use [code] tags
u could do it all in one page in stead of 3 pages. some info about restricting uploads
http://www.w3schools.com/php/php_file_upload.asp
use this on db inputs - mysql_real_escape_string ($user_input)
http://uk3.php.net/manual/en/functio...ape-string.php
for the $uploadDir i would not add this to db value as it will save on db space and if you want to change image dir u will have more flexibility to do so, that is of course the $uploadDir is constant and none changing for all images.
http://www.w3schools.com/php/php_file_upload.asp
use this on db inputs - mysql_real_escape_string ($user_input)
http://uk3.php.net/manual/en/functio...ape-string.php
for the $uploadDir i would not add this to db value as it will save on db space and if you want to change image dir u will have more flexibility to do so, that is of course the $uploadDir is constant and none changing for all images.
PHP Syntax (Toggle Plain Text)
<?php $uploadDir = 'upload/'; if(isset($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { $err= "Error uploading file"; }else{ // upload good do db include 'includes/config.php'; include 'includes/opendb.php'; if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $query = "INSERT INTO contacts (first, name, size, type, path ) ". "VALUES ('$first', '$fileName', '$fileSize', '$fileType', '$filePath')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); include 'includes/closedb.php'; $err= "<br>Files uploaded<br>"; } } ?> <?php echo $err; ?> <form method="post" enctype="multipart/form-data"> first <input type="text" name="first" id="first" /><br/> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> filename <input name="userfile" type="file" id="userfile"><br /> <input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </form>
Last edited by amigura; May 11th, 2008 at 8:38 am.
i don't know if it will work but you need to pass file vars to form 2 then on to form 3
you should have upload on form 2 or 3 for safety reason.
Page 2:
you should have upload on form 2 or 3 for safety reason.
Page 2:
PHP Syntax (Toggle Plain Text)
<form action="insert.php" method="post"> <input type="text" name="first" id="first" /> <input type="hidden" name="tempfile" id="tempfile" value="<?php echo $_FILES['userfile']['tmp_name']; ?>" /> <input type="hidden" name="filename" id="filename" value="<?php echo $_FILES['userfile']['name']; ?>" /> <input type="submit" name="button" id="button" value="Submit" /> </form>
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
I am trying a different approach and I think I am on the right track. I put the file upload script in Page 2 and I am trying to use UPDATE to insert the rest of the information (I hope I am going in the right direction). Now!.... what would be the best way to select the last inserted id or record?
Thank you for the help so far,
Scott
Thank you for the help so far,
Scott
•
•
Join Date: May 2008
Posts: 31
Reputation:
Solved Threads: 5
•
•
•
•
I am trying a different approach and I think I am on the right track. I put the file upload script in Page 2 and I am trying to use UPDATE to insert the rest of the information (I hope I am going in the right direction). Now!.... what would be the best way to select the last inserted id or record?
Thank you for the help so far,
Scott
On loading page 2 of form, upload file, write it to disk and set a session variable with the file path. Also collect the form variables into the session and display form page 2.
On loading page 3 of form, you write the variables from page 2 to session and display the rest of form. On submit of the last page, you take the POST variables along with the form data in session and insert into database in one shot along with file path if necessary, then display the thank you page.
The whole affair should be pretty straightforward and simple to do this way. It's one query and you are collecting the file on the first page.
-r
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
I'd put the file upload in page one like the client originally wanted.
On loading page 2 of form, upload file, write it to disk and set a session variable with the file path. Also collect the form variables into the session and display form page 2.
On loading page 3 of form, you write the variables from page 2 to session and display the rest of form. On submit of the last page, you take the POST variables along with the form data in session and insert into database in one shot along with file path if necessary, then display the thank you page.
The whole affair should be pretty straightforward and simple to do this way. It's one query and you are collecting the file on the first page.
-r
I honestly appreciate everyone's help on this.
Scott
•
•
Join Date: May 2008
Posts: 31
Reputation:
Solved Threads: 5
PHP Syntax (Toggle Plain Text)
#page 1 form processor <?php session_start(); # $uploadDir = 'upload/'; # # if(isset($_POST['upload'])) # { # $fileName = $_FILES['userfile']['name']; # $tmpName = $_FILES['userfile']['tmp_name']; # $fileSize = $_FILES['userfile']['size']; # $fileType = $_FILES['userfile']['type']; # $filePath = $uploadDir . $fileName; # $result = move_uploaded_file($tmpName, $filePath); # if (!$result) { # echo "Error uploading file"; # exit; # } else { $_SESSION['uploadedfilepath']=$filePath; } ## read form vars here into session $_SESSION['formpage1']=$_POST; ?> <!-- display form page 2 here -->
Then collect each form post page like that (sans the file stuff for the rest of the pages).
At the end you'll have 2 arrays in session and the file upload path and you can do this when submitting the final page.
PHP Syntax (Toggle Plain Text)
session_start(); function filterbadstuff($value) { /*filter out xss, sql injection etc here so your form doesn't get hacked*/ return $filteredvalue; } $filteredformvars=array(); while(list($fieldname, $fieldvalue)=each($_SESSION['formpage1'])) { $filteredformvars[$fieldname]=filterbadstuff($fieldvalue); } while(list($fieldname, $fieldvalue)=each($_SESSION['formpage2'])) { $filteredformvars[$fieldname]=filterbadstuff($fieldvalue); } while(list($fieldname, $fieldvalue)=each($_POST)) { $filteredformvars[$fieldname]=filterbadstuff($fieldvalue); }
At this point $filteredformvars has all of your form data in a neat array. I don't know what your data looks like but you should use php filters to scrub the data before building a query.
Just filter the data in that filterbadstuff() function.
Otherwise a nice injection can allow someone to steal your customer data. Don't forget to call the session_start(); at the top of every page where you are getting data to and from session.
Check out the php data filtering as well as mysql functions for this. Data scrubbing is way beyond the scope of a forum post. This code is not debugged. It's meant to show you some ways of handling this.
By hacking this out yourself you'll learn important stuff...
-r
Last edited by rgviza; May 14th, 2008 at 3:50 am.
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
Excellent! Thank you for the help..... I am getting a blank page on Page 2 though.....
Here is Page 1
Am I missing something simple?
-Scott
Here is Page 1
php Syntax (Toggle Plain Text)
<?php session_start(); $_SESSION['userfile']; ?> <html> <body> <form action="page2.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> <input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </form> </body> </html> Page 2 <?php session_start(); $_SESSION['userfile']; $uploadDir = 'upload/'; if(isset($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } else { $_SESSION['uploadedfilepath']=$filePath; } $_SESSION['userfile']=$_POST; ?> <html> <body> <form action="insert.php" method="post"> <input type="text" name="first" id="first" /> <input type="submit" name="button" id="button" value="Submit" /> </form> </body> </html>
Am I missing something simple?
-Scott
Last edited by peter_budo; May 14th, 2008 at 3:58 pm. Reason: Keep It Organized - please use [code] tags
![]() |
Similar Threads
- Preview an image before upload (JavaScript / DHTML / AJAX)
- File upload (PHP)
- Upload image from imagefield (ASP.NET)
- pass variables across pages (PHP)
- Uploading multiple Images through input type="file" (PHP)
- Receiving closed filehandle warning (Perl)
- File Attachment Small Problem (PHP)
- Automating an FTP upload (Shell Scripting)
- vc++ mfc-i can't make getline work with a string for file input (C++)
Other Threads in the PHP Forum
- Previous Thread: Page Navigation
- Next Thread: Display Username
| Thread Tools | Search this Thread |
# 5.2.10 action address apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display dissertation domain dynamic echo echo$_get[x]changingitintovariable... email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image images include insert ip javascript joomla legislation limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop open paypal pdf persist php popup problem query radio random record recursion remote script search server sessions sms sockets source space sql syntax system table tutorial update upload url validator variable video web youtube





