dmikester1 Posted May 4, 2012 Share Posted May 4, 2012 I need some help with emailing an uploaded file. The file is always corrupted and just a few bytes, when it is normally a few KB. I would prefer not to use the PHP mailer class so I can learn myself how to do the coding. Thanks Mike Here is the relevant code: $uploaded = 0; $messages = array(); if(isset($_FILES['uploadFiles']['name'])) { foreach ($_FILES['uploadFiles']['name'] as $i => $name) { if ($_FILES['uploadFiles']['error'][$i] == 4) { continue; } if ($_FILES['uploadFiles']['error'][$i] == 0) { if ($_FILES['uploadFiles']['size'][$i] > 99439443) { $messages[] = "$name exceeded file limit."; continue; } $uploaded++; } } $files = $_FILES['uploadFiles']['tmp_name']; } echo "$uploaded receipt"; if($uploaded != 1) { echo "s"; } echo " uploaded."; foreach ($messages as $error) { echo $error; } // boundary $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // headers for attachment $eol = PHP_EOL; $headers = "From: ".$to.$eol; $headers .= "Reply-To: ".$to.$eol; $subject = "Expense Report - Period Ending $reportDate"; $headers .= "MIME-Version: 1.0".$eol; $headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol; // no more headers after this, we start the body! // //$body = "--".$mime_boundary.$eol; //$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; $body .= "This is a multi-part message in MIME format.".$eol; // message $body .= "--".$mime_boundary.$eol; $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; $body .= $message.$eol.$eol; if($uploaded > 0) { // if there are attachments add this line to the message, otherwise with no attachments this line will add an unwanted attachment //$message .= "--{$mime_boundary}\n"; } if(isset($_FILES['uploadFiles']['name'])) { // preparing attachments for($x=0;$x<count($files);$x++){ $file = fopen($files[$x],"rb"); $data = fread($file,filesize($files[$x])); fclose($file); $data = chunk_split(base64_encode($data)); $name = $_FILES['uploadFiles']['name'][$x]; /*$body .= "–{$mime_boundary}\n"; $body .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; //$body .="--{$mime_boundary}--\n"; if ($x==count($files)-1) { //$message .="--{$mime_boundary}-\n"; } else{ //$message .="--{$mime_boundary}\n"; }*/ // attachment $body .= "--".$mime_boundary.$eol; $body .= "Content-Type: application/octet-stream; name=\"".$name."\"".$eol; $body .= "Content-Transfer-Encoding: base64".$eol; $body .= "Content-Disposition: attachment".$eol.$eol; $body .= $data.$eol.$eol; $body .= "--".$mime_boundary."--"; } } // send $ok = @mail($to, $subject, $body, $headers); if ($ok) { echo "<p>email sent to $to!</p>"; } else { echo "<p>email could not be sent!</p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262077-emailing-an-uploaded-file-file-corrupted/ Share on other sites More sharing options...
dmikester1 Posted May 4, 2012 Author Share Posted May 4, 2012 Do I have to move the uploaded file to a permanent location or can I just use the 'tmp_name' to email it? Quote Link to comment https://forums.phpfreaks.com/topic/262077-emailing-an-uploaded-file-file-corrupted/#findComment-1343102 Share on other sites More sharing options...
dmikester1 Posted May 4, 2012 Author Share Posted May 4, 2012 Heh, figured this one out on my own. Turns out for my situation, I had to do a "move_uploaded_file" to a folder in my web root for it to work. Maybe this isn't always the case, but it fixed my problem. I also had to move the last "$body .= "--".$mime_boundary."--";" outside of the for loop to get it to work with multiple files. Maybe this will help someone else out. Quote Link to comment https://forums.phpfreaks.com/topic/262077-emailing-an-uploaded-file-file-corrupted/#findComment-1343141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.