我已经为一个类(class)项目(不是 CS 类(class))部署了一个网站,并且正在制作一个联系表格,要求用户提供他们的基本用户信息和照片。但是,根据表格提交时,我得到一个由我的 if 语句回显的错误。
我使用的教程来自:https://www.youtube.com/watch?v=zYocypr0Xig .我根据 https://www.w3schools.com/php/php_file_upload.asp 在 php 文件所在的目录中创建了一个名为“tmp_name”的目录.我环顾四周 10 多个小时,找不到解决方案或更简洁的示例来说明原因,就像 Youtube 视频那样。
HTML:
<form action="betacontactform.php" method="post" enctype="multipart/form-data">
<div class="form-container">
<div class="columns" style="width: 500px; ">
<h3 id="intro-box-headers">User Details:</h3>
* First Name: <input type="text" name="firstName" placeholder="Jane" required>
Last Name: <input type="text" name="lastName" placeholder="Doe">
* Email: <input type="text" name="email" placeholder="jdoe@example.com" required>
* County: <input type="text" name="county" placeholder="County" required>
* State: <input type="text" name="state" placeholder="State" required>
</div>
<span>* At least 1 picture of your project must be uploaded:</span>
<input type="file" name="attachment" placeholder="Upload Images (required):" required/>
<!-- TODO: multiple optional image file uploads -->
<br>
</div>
</div>
<div style="width: 100%; text-align: center;">
<input style="margin-left: 0px;" class="submit-form-button" type="submit" name="submit" value="Submit" />
</div>
</form>
PHP:
<?php
if (isset($_POST['submit']) && isset($_FILES['attachment'])) {
// $message contents
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$county = $_POST['county'];
$state = $_POST['state'];
// image upload attachment
// store some variables
$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['tmp_name'];
$file_type = $_FILES['attachment']['type'];
// get the extension of the get_included_files
$base = basename($file_name);
$extension = substr($base, strlen($base)-4, strlen($base));
// only these file types will be allowed
$allowed_extensions = array(".png","jpeg",".jpg");
// mail essentials
$from = $_POST['email'];
$to = "myemail@gmail.com";
$subject = "New Client Alert!!";
$message = "You have received an email from $firstName $lastName \n Location: $county $state";
// things you need
$file = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqueid(time()));
// standard email headers
$header = "From: ".$from."\r\n";
$header .= "Reply-To".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
// declaring we have multiple kinds of email (i.e. plain text and attachment)
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
// plain text part
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
// file attachment part
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
$header .= $content."\r\n\r\n";
mail($to, $subject, "", $header);
header("Location: /website/postformpage.html");
} else {
echo "Error!";
}
我希望它能通过,但我不确定它为什么会输出错误以及我搞砸的地方。
最佳答案
也许是因为您希望提交帖子数据?
isset($_POST['提交']) ?
把它取下来或者把按钮改成输入
<button style="margin-left: ;" class="submit-form-button" type="submit" name="submit">Submit!</button>
to
<input style="margin-left:0;" class="submit-form-button" type="submit" name="submit" value="Submit"/>
关于php - 如何使用 mail() 或 PHPMailer 创建带有文件图像上传的 HTML5 电子邮件联系表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55408821/