到目前为止,我已经尝试了很多我在SO中看过的例子,没有任何效果。所以我希望别人能用不同的光和解决方案来看待我的榜样。
我有一个文件上传输入字段,只接受png和jpg。如果它不是,则发送到错误页面。但是,这是交易,我不能把它放在if-else条件语句中,因为整个表单已经在try catch中,应该进行错误验证。所以,我的问题是,我如何在那里工作,以便它仍然可以验证图像是否输入(ed)或如果为空则忽略它?
这是我在sendmail.php中的代码
<?php
date_default_timezone_set('Etc/UTC');
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
require_once('../recaptchalib.php');
$privatekey = "###########################";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// Build a page for this
header('location: /error.php');
die();
} else {
$mail = new PHPMailer(true);
$mail->isSMTP();
try{
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = "@@@@@@@@@gmail.com";
$mail->Password = "#######";
$name = $_POST['full_name'];
$email = $_POST['email'];
$mail->isHTML(true);
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email, $name);
//CODE IN QUESTION
if ($_FILES['logo']['type'] == "application/png" || $_FILES['logo']['type'] == "application/jpg") {
$mail->AddAttachment($_FILES['logo']['tmp_name'], $_FILES['logo']['name']);
}
else {
?>
<script language="javascript" type="text/javascript">
window.location = '/error.php';
</script>
<?php
}
$mail->addAddress('@@@@@@@@@outlook.com', 'First Last');
$mail->Subject = '@@@@@@@@@@@@@@@@@@@';
$mail->MsgHTML($body);
$mail->Send();
$mail->ClearAddresses();
?>
<script language="javascript" type="text/javascript">
window.location = '/thankyou.php';
</script>
<?php
} catch (phpmailerException $e) {
?>
<script language="javascript" type="text/javascript">
window.location = '/error.php';
</script>
<?php
}
}
?>
为简单起见,我们可以假设HTML是:
<input type="file" name="logo"/>
提前谢谢
编辑(补充)
实际上向我发送文件的另一种方法是下面的文件,但是,它不会尊重它不应该发送未经授权的文件并转到错误屏幕的验证。基本上,它会发送你放在那里的任何东西。 [if-else声明的替代]
$allowedExts = array("zip", "png", "jpg", "jpeg");
$temp = explode(".", $_FILES["logo"]["name"]);
$extension = end($temp);
if ((($_FILES["logo"]["type"] == "application/png")
|| ($_FILES["logo"]["type"] == "application/jpg"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["logo"]["error"] > 0)
{
echo "<script>alert('Error: " . $_FILES["logo"]["error"] ."')</script>";
}
else
{
$d='upload/';
$de=$d . basename($_FILES['logo']['name']);
move_uploaded_file($_FILES["logo"]["tmp_name"], $de);
$fileName = $_FILES['logo']['name'];
$filePath = $_FILES['logo']['tmp_name'];
}
}
$mail->AddAttachment($_FILES['logo']['tmp_name'], $_FILES['logo']['name']);