Jump to content

Passing values of a file upload?


2xldesign

Recommended Posts

I have been looking everywhere for help on this and I have had no luck. I have a multi page form where a file upload needs to be on the first page. I need to pass the file upload values to the upload script on the last page. If someone could point me in the right directions that would be great. Here is a rough example of my pages:

 

Page 1

 

<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

 

<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

$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>";

}

?>

 

How do I carry sessions?

 

Thanks!

 

Scott

Link to comment
Share on other sites

So...The first form comes up, the user selects the file, and you store the information.  The next form comes up, they fill out more information, and the third page comes up, which uploads the file and inserts the user information?

 

I just want to understand exactly what is going on so I can try to help you the best I can.

Link to comment
Share on other sites

So...The first form comes up, the user selects the file, and you store the information.  The next form comes up, they fill out more information, and the third page comes up, which uploads the file and inserts the user information?

 

I just want to understand exactly what is going on so I can try to help you the best I can.

 

Correct... I have been told that passing the file values is not possilbe, and I have been told it is using sessions. I have searched and searched. I know it is possible because I have seen it done many times.

 

Thank you for your help so far, I appreciate it.

 

-Scott

Link to comment
Share on other sites

It is interesting you are still trying to solve this. In your previous thread I was the one who stated you needed to move the uploaded file into a session variable - http://www.phpfreaks.com/forums/index.php/topic,192929.0.html

 

The code necessary to do this is trivial.

 

Page2 -

 

<?php
// put uploaded file information ($_FILES) and the uploaded file ($_FILES['userfile']['tmp_name'] ) into a session
session_start(); // start new or resume an existing session

// do some minimal error checking if the file is actually an uploaded file
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
   $_SESSION['actual_file'] = file_get_contents($_FILES['userfile']['tmp_name']); // read and put the file into the session
   $_SESSION['files_array'] = $_FILES; // copy the $_FILES array into the session (the ['tmp_name'] entry will be invalid after the end of this page, do not use 'tmp_name')
}
?>
<form action="page3.php" method="post">
<input type="text" name="first" id="first" />
<input type="submit" name="button" id="button" value="Submit" />
</form>

 

Page3 -

 

<?php
session_start();
// display the contents of the upload information saved in the session
echo "<pre>",print_r($_SESSION,true),"</pre>";
// use $_SESSION['actual_file'] and $_SESSION['files_array']['userfile']['name'] or ['type'] or ['error'] or ['size'] any way you want in your code

?>

Link to comment
Share on other sites

<?php

// put uploaded file information ($_FILES) and the uploaded file ($_FILES['userfile']['tmp_name'] ) into a session

session_start(); // start new or resume an existing session

 

// do some minimal error checking if the file is actually an uploaded file

if (is_uploaded_file($_FILES['userfile']['tmp_name']))

{

  $_SESSION['actual_file'] = file_get_contents($_FILES['userfile']['tmp_name']); // read and put the file into the session

  $_SESSION['files_array'] = $_FILES; // copy the $_FILES array into the session (the ['tmp_name'] entry will be invalid after the end of this page, do not use 'tmp_name')

}

?>

 

Does not work..... All I get is a blank page....

 

Thanks you though,

 

Scott

Link to comment
Share on other sites

The page code I posted outputs something (a form). The code you posted does not output anything.

 

I apologize...

 

I think I am too far out of my league here. I appreciate the help, but I have no idea what I am doing with the code you gave me. The funny thing is this is probably basic stuff and I have managed to overly confuse it.

 

Hats off to you for knowing this stuff....... LOL  :)

Link to comment
Share on other sites

If you are not able to read and identify the purpose of each of the posted lines of code, then you have probably jumped into this without learning the basics first. This will result in the most frustrating experience possible and take you a really long time to produce code that accomplishes anything.

 

I recommend buying a basic book on php and/or HTML.

 

The tutorials at w3schools are also fairly useful for learning the basics - http://www.w3schools.com/default.asp

 

Since we don't know your experience level or the level of understanding of what you and I have posted in this thread, you would need to ask a specific question if you want specific help with something that has been posted.

Link to comment
Share on other sites

i have taught myself the basics..... basic insert into database, sessions , includes etc. i usually just take tutorials and edit them to work for me. i have looked EVERYWHERE for an example of what i am trying to accomplish. i was hoping someone could post a link to say "oh that is simple, do this and this".

 

Thanks again,

 

Scott

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.