Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SChaput

macrumors regular
Original poster
Jul 2, 2008
153
0
United States
Hello All,

I'm trying to establish a form where a user is able to select a file they want to upload to my server. However, my form has multiple steps involved, and I would like to have the user select the file they want uploaded, go through a few more steps and then, when they click the final submit, it uploads the file. Is there a way to do this? I've got the script working perfectly if I upload straight away, but am unable to hold it until later. Any advice?
Code:
Code:
<form action="computer" name="custom" method="post" enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<input name="uploadedfile" type="file" size="30" />
</form>

To Process the form i use:
Code:
$target_path = "files/";
		$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
		$_SESSION['target_path'] = $target_path;
		$_SESSION['tempinfo'] = $_FILES['uploadedfile']['tmp_name'];
And then later when i upload it i do this
Code:
if(move_uploaded_file($_SESSION['tempinfo'], $_SESSION['target_path'])) {
    echo "The file has been uploaded!";
} else{
    echo "There was an error uploading the file, please try again!";
}
 
Simple solution: Have the user upload the file on the last step. When you use an <input type="file">, I don't believe you have any option to "hold" the file or filename until later.

-Aaron-
 
The $_FILES information is only good for a single page load, so trying to get back it at on a later step won't work (it would be a security issue if this wasn't the case). You'll need to upload it to a temporary location until the last step, or as suggested above, have the user do the upload on the last step.
 
Thanks for the responses! I decided to go with aarond12's solution to just have them upload the file at the final step. great idea, can't believe i didn't think of it. Now however, i'm trying to attach that file to a mail() command. I've got the below code
I"m trying to attach a .pdf file to a php mail() command, and having alot of difficulties. Here is my code:
Code:
move_uploaded_file($_SESSION['tempinfo'], $_SESSION['target_path']);
  
$file = fopen($_SESSION['target_path'], 'rb');
$data = file_get_contents($_SESSION['target_path'], filesize($_SESSION['target_path']));
$attachment = chunk_split(base64_encode(file_get_contents($_SESSION['target_path']))); 

	$random_hash = md5(date('r', time())); 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";


$message .= "--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/pdf; name='Something.pdf'
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>-- 
";

THe file is uploaded and is in the correct location, but when the email comes through it comes in all giberish. and no attachment.

any suggestions to that please?

thanks again
 
This doesn't do what you think it does. The <?php ... ?> parts are not executed. Just use the variable names. Looks like you tried to copy code from this page, but didn't get the pieces right.
PHP:
$message .= "--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/pdf; name='Something.pdf'
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>-- 
";
Edited version of your code below. Notice the syntax coloring differences. Syntax coloring is a good way to spot syntax errors.
PHP:
$message .= "--PHP-alt-$random_hash--

--PHP-mixed-$random_hash
Content-Type: application/pdf; name='Something.pdf'
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--PHP-mixed-$random_hash-- 
";
 
Thanks for the quick reply's, angelwatt. However, there seems to be something that's still causing me an issue. The file gets attached to the email, and I am able to see the file, with the name and the size. However, when I click the attachment to try and open it it says it could not be opened due to a possible error in decoding the attachment? Any suggestions for this error? Thanks

Here's my code one more time:
Code:
$attachment = chunk_split(base64_encode(file_get_contents($_SESSION['target_path']))); 
$random_hash = md5(date('r', time())); 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-$random_hash\"";

$test .= "--PHP-alt-$random_hash--

--PHP-mixed-$random_hash
Content-Type: application/pdf; name=Something.pdf
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--PHP-mixed-$random_hash-- 
";
 
Try changing the Content-type from application/pdf to application/octet-stream

I haven't done any attachments in email so I'm not sure about some of the specifics here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.