PHP表单错误 - HTML和图像上传

[英]PHP Form Error - HTML & Image upload


So a bit of quick background. I've only just started getting into php and i've been on this for a few days now. Apologies if my code is messy but I've tried to tag as much as i can.

所以有点快速的背景。我刚刚开始进入php,我已经在这几天了。抱歉,如果我的代码很乱,但我试图尽可能多地标记。

I have a form where on completion, 3 things are supposed to happen.

我有一个表格在完成时,应该发生3件事。

  1. Email is sent to the recipient with all the details that were filled out are sent through formatted nicely in a html table + image attachment.
  2. 电子邮件将发送给收件人,所有填写的详细信息都通过html表+图像附件中的格式很好地发送。

  3. Plain text email is sent to the form applicant with a thank you.
  4. 纯文本电子邮件将发送给表单申请人,谢谢。

  5. Also, on success of form submission the form applicant is then automatically directed to a "thank you" page.
  6. 此外,在表单提交成功后,表单申请者将自动转到“谢谢”页面。

• All is fine if all required fields filled in AND image is uploaded/attached. The whole process is run without error.

•如果上传/附加了填写AND图像的所有必填字段,则一切正常。整个过程运行无误。

• If an image is NOT uploaded/attached, I get the following message

•如果没有上传/附加图像,我会收到以下消息

Warning: file(): Filename cannot be empty in /home/website/public_html/repairs/contact.php on line 49

警告:file():在第49行的/home/website/public_html/repairs/contact.php中,文件名不能为空

Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/repairs/contact.php:49) in /home/website/public_html/repairs/contact.php on line 108

警告:无法修改标题信息 - 已在第108行/home/website/public_html/repairs/contact.php中发送的标题(由/home/website/public_html/repairs/contact.php:49开始输出)

followed by an email received that is seen with all the html tags/code/plain text.

接着是收到的电子邮件,其中包含所有html标签/代码/纯文本。

The plain text email sent to the "form applicant" with a thank you is still received.

发送给“表格申请人”的纯文本电子邮件仍然收到了谢谢。

See my php code below

请参阅下面的PHP代码

<?php

 // multiple recipients
$to = $_REQUEST['sendto'] ; 
$from = $_REQUEST['email'] ;

// subject
$subject = "Repairs Request Form"; 
$fname = $_REQUEST['fname'] ; 

$message = '
<html>
<body>
<table rules="all" style="border-color: #666; border="1px" "cellpadding="10">
<tr><td style="background: #eee;" colspan="2"><strong>Repairs Request Enquiry</strong></td></tr>
<tr><td><strong>First Name:</strong> </td><td>' . strip_tags($_POST['fname']) . '</td></tr>
<tr><td><strong>Last Name:</strong> </td><td>' . strip_tags($_POST['lname']) . '</td></tr>
<tr><td><strong>Email:</strong> </td><td>' . strip_tags($_POST['email']) . '</td></tr>
<tr><td><strong>Contact Number:</strong> </td><td>' . strip_tags($_POST['phone']) . '</td></tr>
<tr><td><strong>Address:</strong> </td><td>' . strip_tags($_POST['address']) . '</td></tr>
<tr><td><strong>Suburb:</strong> </td><td>' . strip_tags($_POST['suburb']) . '</td></tr>
<tr><td><strong>Postcode:</strong> </td><td>' . strip_tags($_POST['postcode']) . '</td></tr>
<tr><td><strong>Type of repair:</strong> </td><td>' . strip_tags($_POST['repair']) . '</td></tr>
<tr><td><strong>Nature of the problem:</strong> </td><td>' . $_POST['problem'] . '</td></tr>
<tr><td><strong>Is this urgent:</strong> </td><td>' . $_POST['urgent'] . '</td></tr>
<tr><td><strong>Type of Urgency:</strong> </td><td>' . $_POST['urgent_type'] . '</td></tr>
<tr><td><strong>Master key access required:</strong> </td><td>' . $_POST['masteraccess'] . '</td></tr>
</table>
</body>
</html>'
;

 //ATTACHMENT START

/* GET File Variables */ 
$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']; 
$fileName = $_FILES['attachment']['name']; 

/* Start of headers */ 
$headers = "From: $from"; 

//**XXXXXXXXXXXXXXXX  PROBLEM IS HERE XXXXXXXXXXXXXXXX**    

if (file($tmpName)) { 
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb'); 
  $data = fread($file,filesize($tmpName)); 
  fclose($file); 


  /* a boundary string */
  $randomVal = md5(time()); 
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n"; 
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\""; 

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n" . 
  "--{$mimeBoundary}\n" . 
  "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
  "Content-Transfer-Encoding: 7bit\n\n" . 
  $message . "\n\n"; 

  /* Encoding file data */
  $data = chunk_split(base64_encode($data)); 

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" . 
  "Content-Type: {$fileType};\n" . 
  " name=\"{$fileName}\"\n" . 
  "Content-Transfer-Encoding: base64\n\n" . 
  $data . "\n\n" . 
  "--{$mimeBoundary}--\n"; 
} 
 //END OF ATTACHMENT


  //Message/email to the customer.
 $headers2 = "From: noreply@website.com.au"; 
 $subject2 = "Thank you. Your e-mail has been logged."; 
 $autoreply = "Thank you. Your e-mail has been logged.
We will attend to your request and be in touch shortly.
Rest assured, we are doing all we can to speed up this process.
Regards,
Repairs Team";


 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($fname == '') {print "You have not entered your first name";} 
 else { 
 $send = mail($to, $subject, $message, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {header( "Location: http://www.website.com.au/thankyoupage/thankyou.html" );} 
 else 
 {print "We encountered an error sending your mail, please notify webmaster@website.com.au"; } 
 }
}
?>

I tried to follow this post but I'm just loosing it. Php form upload and email

我试着关注这篇文章,但我只是放弃它。 Php表单上传和电子邮件

Any help is much appreciated.

任何帮助深表感谢。

2 个解决方案

#1


0  

You cannot send headers to redirect a page if they have already been set.

如果页面已经设置,则无法发送标题以重定向页面。

You can try to flush the object by using ob_flush() at the start of your page.

您可以尝试使用页面开头的ob_flush()来刷新对象。

#2


0  

change

if (file($tmpName)) {

to

if($_FILES["attachment"]["error"] != 0) {
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/09/04/7dee348d26f5e9f2025198b5a256643b.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告