在php中验证文件附件只有doc和pdf

时间:2014-04-08 13:00:55

标签: javascript php html

在下面的代码中,它正在上传所有文件类型,但我只需要doc和pdf文件

我想只上传doc和pdf文件帮助我的朋友们我可能知道该怎么做

这里的代码是指任何说明如何验证。

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Attachment Without Upload - Excellent Web World</title>
<style>
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;}
th{ background:#999999; text-align:right; vertical-align:top;}
input{ width:181px;}
</style>
</head>
<body>
    <form action="emailSend.php" method="post" name="mainform" enctype="multipart/form-data">
    <table width="500" border="0" cellpadding="5" cellspacing="5">
       <tr>
        <th>Your Name</th>
        <td><input name="fieldFormName" type="text"></td>
    </tr>
    <tr>
    <tr>
        <th>Your Email</th>
        <td><input name="fieldFormEmail" type="text"></td>
    </tr>
    <tr>
        <th>To Email</th>
        <td><input name="toEmail" type="text"></td>
    </tr>

    <tr>
        <th>Subject</th>
        <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
    </tr>
    <tr>
        <th>Comments</th>
        <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
    </tr>
    <tr>
      <th>Attach Your File</th>
      <td><input name="attachment" type="file"></td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
    </tr>
    </table>
    </form>
</body>
</html>

emailSend.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php

$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail']; 
$fromName = $_POST['fieldFormName']; 
$subject = $_POST['fieldSubject']; 
$message = $_POST['fieldDescription'];

/* GET File Variables */ 
$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']==doc/pdf; 
$fileName = $_FILES['attachment']['name']; 

/* Start of headers */ 
$headers = "From: $fromName"; 

if (file($tmpName)) { 
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb'); 
  $data = fread($file,filesize($tmpName)); 
  fclose($file); 

  /* a boundary string */
 $randomVal = md5(time()); 
 $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

  /* Header for File Attachment */
 // $headers .= "\nMIME-Version: 1.0\n"; 
 // $headers .= "Content-Type: multipart/mixed;\n" ;
 // $headers .= " boundary=\"{$mimeBoundary}\""; 

  /* Multipart Boundary above message */
  $message = 



  $message . "\n\n"; 

  /* Encoding file data */
  $data = chunk_split(base64_encode($data)); 

  /* Adding attchment-file to message*/
  $message .=  

  " name=\"{$fileName}\"\n" . 

  $data . "\n\n"  
  ; 
} 

$flgchk = mail ("$to", "$subject", "$message","$headers"); 

if($flgchk){
  echo "A email has been sent to: $to";
 }
else{
  echo "Error in Email sending";
}
?>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

您可以按以下方式查看

$allowedExts = array(
  "pdf", 
  "doc", 
  "docx"
); 

$allowedMimeTypes = array( 
  'application/msword',
  'application/pdf'
);

$extension = end(explode(".", $_FILES["file"]["name"]));


if ( ! ( in_array($extension, $allowedExts ) ) ) {
  die('Please provide another file type .');
}

if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) ) 
{      
 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); 
}
else
{
die('Please provide another file type .');
}

答案 1 :(得分:0)

首先检查上传文件的MIME类型,然后检查是否存在允许的类型。

如何检查MIME类型..

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $_FILES['file']['tmp_name']);
echo $mimetype;

您的$allowedrules array,也请使用此代码检查php中的doc and pdf

$allowedrules = array( 
  'application/msword',
  'text/pdf',
);

更新

否则使用此条件,它仅限制.doc&amp; .pdf文件。

<?php

    $allowedExts = array("pdf", "doc", "docx");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts))
    {
      if ($_FILES["file"]["error"] > 0)
      {
         echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
      }
      else
      {
        echo "Success";
      }
  }

答案 2 :(得分:0)

抱歉,我知道你只想要一个php答案,但你可以用jQuery来保存服务器资源。像这样:

$("input:file").change(function () {
     if ($(this).val() !== "") {
          var ul_file = $(this).val();
          var extension = ul_file.substr((ul_file.lastIndexOf('.') + 1));
          var accepted_file_endings = ["doc", "docx", "doc"];
          extension = extension.toLowerCase();
          if ($.inArray(extension, accepted_file_endings) !== -1) {
          //start upload
          ...
          }
     }
});