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) {

注意!

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



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