发送前检查文件类型,如果不允许则不要继续?

时间:2015-03-24 15:35:08

标签: php

如何在发送电子邮件之前添加支票是否允许上传的文件类型?

我将从不同的文件输入上传文件(1个输入中没有多个文件),当任何这些文件不是.jpg,.doc

时我不想继续

怎么做?

if (is_uploaded_file ($_FILES['attachment']['tmp_name'])) {
  $mail->AddAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'], 'base64',$_FILES['attachment']['type']);
}

1 个答案:

答案 0 :(得分:0)

您可以使用strstr()获取临时文件格式

if (is_uploaded_file ($_FILES['attachment']['tmp_name'])) {

      $check = strstr($_FILES['attachment']['tmp_name'], "."); //get the file format
      if(($check == ".jpg") || ($check == ".doc")){
           //$mail->AddAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'], 'base64',$_FILES['attachment']['type']);
           print 'good';
      }else{
           echo $check." is not acceptable";
      }
}