OK I have a form script that is meant to send me a file that has been uploaded It works fine except for the issue of redirecting after the form has been posted I am getting this error Warning: Cannot modify header information - headers already sent by (output started at /****/form.php:1) in /mounted-storage/home101c/sub005/sc62881-JLWT/zqip.com/ninja/form.php on line 86 line 86 is the header header("Location: http://www.example.com/success"); Does anyone know what the problem is or how to fix it r another way to redirect to a thank you page all the code is <?php /* Mailer with Attachments */ $action = ""; $action = $_REQUEST['action']; global $action; function showForm() { ?> <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>"> <input type="hidden" name="action" value="send" /> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <p>Name: <input name="from_name" size="40" /></p> <p>Email: <input name="from_email" size="40" /></p> <p>Subject: <input name="subject" size="40" /></p> <p>Message: <textarea name="body" rows="5" cols="31"></textarea></p> <p>Attachment: <input type="file" name="attachment" size="30" /></p> <p><input type="submit" value="Send Your CV" /></p> <?php } function sendMail() { $from_name = stripslashes($_POST['from_name']); $subject = stripslashes($_POST['subject']); $body = stripslashes($_POST['body']); $attachment = $_FILES['attachment']['tmp_name']; $attachment_name = $_FILES['attachment']['name']; // check for valid file types if (is_uploaded_file($attachment)) { //Do we have a file uploaded? $allowedExtensions = array("doc","docx","txt"); foreach ($_FILES as $file) { if ($file['tmp_name'] > '') { if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) { die($file['name'].' is an invalid file type! '. ''. '<< Go Back'); }else{ $fp = fopen($attachment, "rb"); //Open it $data = fread($fp, filesize($attachment)); //Read it $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed fclose($fp); } } } } //Let's start our headers $headers = "From: $from_name<" . $_POST['from_email'] . ">\n"; $headers .= "Reply-To: <" . $_POST['from_email'] . ">\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; $headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n"; $headers .= "X-Mailer: PHP4\n"; $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: <" . $_POST['from_email'] . ">\n"; $headers .= "This is a multi-part message in MIME format.\n"; $headers .= "------=MIME_BOUNDRY_main_message \n"; $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; $message = "------=MIME_BOUNDRY_message_parts\n"; $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; $message .= "Content-Transfer-Encoding: quoted-printable\n"; $message .= "\n"; /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */ $message .= "$body\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_message_parts--\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message\n"; $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n"; $message .= $data; //The base64 encoded message $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message--\n"; // send the message mail("xxx@hotmail.com", $subject, $message, $headers); header("Location: http://www.example.com/success"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <html> <head> </head> <body> <div id="right"> <?php switch ($action) { case "send": sendMail(); showForm(); break; default: showForm(); } ?> </div> </body> </html> PHP:
You cannot send headers in the middle of a script. Headers go on top. That being said, you can circumvent this by adding output buffering to the page. Have a look here: http://php.net/manual/en/function.ob-start.php The output buffer is started by adding ob_start(); at the very beginning of the file (or, if you load this via other files, at the very beginning of the parent file).
I'd ditch the output buffering and do it this way... <?php /* Mailer with Attachments */ $action = $_REQUEST['action']; function showForm() { ?> <form enctype="multipart/form-data" name="send" method="post" action="<?= $_SERVER['PHP_SELF'] ?>"> <input type="hidden" name="action" value="send" /> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <p>Name: <input name="from_name" size="40" /></p> <p>Email: <input name="from_email" size="40" /></p> <p>Subject: <input name="subject" size="40" /></p> <p>Message: <textarea name="body" rows="5" cols="31"></textarea></p> <p>Attachment: <input type="file" name="attachment" size="30" /></p> <p><input type="submit" value="Send Your CV" /></p> <?php } function sendMail() { $from_name = stripslashes($_POST['from_name']); $subject = stripslashes($_POST['subject']); $body = stripslashes($_POST['body']); $attachment = $_FILES['attachment']['tmp_name']; $attachment_name = $_FILES['attachment']['name']; // check for valid file types if (is_uploaded_file($attachment)) { //Do we have a file uploaded? $allowedExtensions = array("doc", "docx", "txt"); foreach ($_FILES as $file) { if ($file['tmp_name'] > '') { if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) { die($file['name'] . ' is an invalid file type!<br />' . '<br />' . '<< Go Back'); } else { $fp = fopen($attachment, "rb"); //Open it $data = fread($fp, filesize($attachment)); //Read it $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed fclose($fp); } } } } //Let's start our headers $headers = "From: $from_name<" . $_POST['from_email'] . ">\n"; $headers .= "Reply-To: <" . $_POST['from_email'] . ">\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; $headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\n"; $headers .= "X-Mailer: PHP4\n"; $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: <" . $_POST['from_email'] . ">\n"; $headers .= "This is a multi-part message in MIME format.\n"; $headers .= "------=MIME_BOUNDRY_main_message \n"; $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; $message = "------=MIME_BOUNDRY_message_parts\n"; $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; $message .= "Content-Transfer-Encoding: quoted-printable\n"; $message .= "\n"; /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */ $message .= "$body\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_message_parts--\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message\n"; $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n"; $message .= $data; //The base64 encoded message $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message--\n"; // send the message mail("xxx@hotmail.com", $subject, $message, $headers); header("Location: http://www.example.com/success"); exit; } /* * By having the sendMail function called before you output anything to the page the headers can be sent successfully. */ switch ($action) { case "send": sendMail(); break; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <html> <head> </head> <body> <div id="right"> <?php showForm(); ?> </div> </body> </html> PHP: In your code you had the sendMail() function followed by the showForm() function. sendMail sends the browser to another page so the form would never be shown.
Whenever I see people doing header redirects to load a 'success' page or the next step, I can't help but say WHYYYY?!?!? -- and that's the same WHYYYY?!?!? as George Carlins response to the news article about the house robber who raped the 97 year old woman. WHYYYY?!?!? Seriously, "redirecting" -- why the hell not just include success as the result for that page? This constant wasting handshakes just to load what should be the normal result output. Glad you got it sorted, but are you still wasting time with some stupid redirect for no reason?
Usually to get around the impatient user who presses F5 over and over and then complains that the email gets sent more than once.