Phpmailer如何允许用户仅使用PHP上传pdf或docs

时间:2017-04-01 07:33:02

标签: php phpmailer

如何限制用户通过联系表单仅上传PDF或DOCS格式。

我试过但无法解决这个问题。对此的任何帮助都会对我有所帮助。

这是我的代码。

<?php
require('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port     = 587;  
$mail->Username = "";
$mail->Password = "";
$mail->Host     = "smtp.gmail.com";
$mail->Mailer   = "smtp";
$mail->From = $_POST["userEmail"];
$mail->FromName = $_POST['userName'];
//$mail->SetFrom($_POST["userEmail"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("");  
$mail->Subject = "Career request by " . $_POST['userName'];
$mail->WordWrap   = 80;
// /$mail->MsgHTML($_POST["userName"]."\r\n"$_POST["userEmail"]);
$mail->MsgHTML("Applicant Name: ".$_POST['userName']."<br/><br/>Applicant Email: ".$_POST['userEmail']." <br/><br/>Applicant Mobile Number: ".$_POST['userPhone']."<br/><br/> Job Applied For : ".$_POST['jobApplied']);
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']); 
}

$mail->IsHTML(true);

if(!$mail->Send()) {
    echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
    echo "<p class='success'>Contact Mail Sent.</p>";
}   
?>

谢谢..

1 个答案:

答案 0 :(得分:0)

您可以使用多种方式检查上传的文件,例如:

$file = explode('.', strtolower($_FILES['attachmentFile']['name']));
$extension = end($file);
$allowed_types = ["pdf", "docs"];

if (!in_array($extension, $allowed_types)) {
   echo "Allowed types are pdf and docs.";
} else {
   // send your email
}