将文件上传添加到mail.php

时间:2012-10-31 23:10:58

标签: php email html-email

我有联系表格结构。有以下内容:

HTML:

   <form name="feedback_form" method="post" action="" class="feedback_form">
                                                <input type="text" name="field-name" value="İsim" title="İsim" class="field-name form_field">
                                                <input type="text" name="field-email" value="Email" title="Email" class="field-email form_field">
                                                <input type="text" name="field-subject" value="Başlık" title="Subject" class="field-subject form_field">
                                                <textarea name="field-message" cols="45" rows="5" title="Mesajınız..." class="field-message form_field">Mesajınız...</textarea>
                                                <input type="reset" name="reset" id="reset2" value="Temizle" class="feedback_reset">
                                                <input type="button" name="submit" class="feedback_go" id="submit2" value="Gönder">                                    

</form>  

我添加了这些代码用于文件上传:

<label for='uploaded_file'>Upload your picture:</label>
 <input type="file" name="uploaded_file">

我想,用户可以通过此标签上传jpg,png,gif,bmp图片。但我不知道如何重新组织我的mail.php文件?

mail.php:

<?php
header('Content-Type: text/html; charset=utf-8');

function sendFeedback($feedback_email, $feedback_msg, $feedback_name, $feedback_subject, $feedback_file) {


    /* EDIT THIS */
    $admin_email = "mymail@gmail.com";
    if ($feedback_subject == "Subject" || empty($feedback_subject) ) {
        $subj = "Email from your site";
    } else {
        $subj = $feedback_subject;
    }


    /* //EDIT THIS */


    $message = "
    <html>
    <head>
      <title>Websitenizin emaili</title>
    </head>
    <body>
        <p><a href='mailto:".$feedback_email."'>".$feedback_name."</a> send this message:</p>
        <p>".$feedback_msg."</p>
        <p>".$subject."</p>
    </body>
    </html>
    ";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

    if ($feedback_name!=="Name" && $feedback_email!=="Email" && !empty($feedback_email) && !empty($feedback_msg) && !empty($feedback_name) ) {
        if ($feedback_email == "mail_error") {
            echo "<span class='ajaxok'>Geçersiz email adresi.</span>";
        } else {            
            mail($admin_email, $subj, $message, $headers);
            echo "<span class='ajaxok'>Teşekkürler. Mesajınız gönderilmiştir.</span>";  
        }
    } else {
        echo "<span class='ajaxalert'>Lütfen zorunlu alanları doldurunuz.</span>";      
    }


}

sendFeedback($_POST['email'], $_POST['message'], $_POST['name'], $_POST['subject'], $_POST['file']);
?>

1 个答案:

答案 0 :(得分:0)

首先将操作更改为您拥有mail.php文件的目录,如下面的代码所示。然后在mail.php中,你将使用类似于下面PHP代码的东西。

 <form name="feedback_form" method="post" action="mail.php" class="feedback_form">

有一种叫做

的东西
$_FILES['file']['type'];

您可以在以下代码中使用它。

如果您想了解更多信息,请在本网站上回复或阅读更多信息

W3Schools file upload

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>