fileinfo不仅限制pdf

时间:2013-03-28 08:15:02

标签: php fileinfo

我使用以下脚本验证上传的文件是否为pdf,以及是否使用phpmailer发送。它发送电子邮件但没有附件。此外,它还允许我附加非pdf文件。请帮忙。

ob_start();
require("class.phpmailer.php");

if(isset($_FILES['upload']['tmp_name'])){
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime=finfo_file($finfo, $_FILES['upload']['tmp_name']);
if($mime=='application/pdf'){

$message = "some message";
$mail = new PHPMailer();
$mail->From     = ('sample@youdomain.net');
$mail->AddAddress=('sample@youdomain.net');
$mail->Subject  = "Submitted files";
$mail->Body     = $message;
$mail->WordWrap = 50;

foreach($_FILES['upload']['tmp_name'] as $upload) 
if(!empty($upload)) {
$mail->AddAttachment($upload);
}
$mail->Send();

header("Location: thankyou.php");
exit();     
}}

1 个答案:

答案 0 :(得分:0)

目前你有:

if( ... ) { 
    ...
    if($mime=='application/pdf') {
    }
}
// some code you want to be executed
// only if the mime type is application/pdf

但你想要:

if (...) {
  ..
  if ($mime=='application/pdf') {
        // place the code you want to be executed
        // only if the mime type is application/pdf
        // here - before the closing }
    }
}