如何将 PHP 表单邮件附件限制为某些文档类型?

发布于 2024-12-05 11:38:37 字数 2614 浏览 1 评论 0原文

我想修改我目前使用的简历上传器代码,将申请人附件的文档类型限制为 doc、docX、rtf 和 PDF。我根据我在 SourceForge.net 上找到的内容修改了此内容。

我还想在成功提交后重定向。现在完成后页面是空白的。我大概能弄清楚。

我的 PHP 技能不太好,所以我真的不知道在哪里放置文件类型验证代码。

<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){

    $to="[email protected]";
    $subject= stripslashes($_POST['apply']);

   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

   $message= stripslashes($_POST['msg']);

   $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";

   foreach($_FILES as $userfile){
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];

      if (file_exists($tmp_name)){

         if(is_uploaded_file($tmp_name)){

            $file = fopen($tmp_name,'rb');
            $data = fread($file,filesize($tmp_name));
            fclose($file);

            $data = chunk_split(base64_encode($data));
         }

          $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n";
           }
   }

   $message.="--{$mime_boundary}--\n";
   if (@mail($to, $subject, $message, $headers))
     echo '<script type="text/javascript">alert("Resume Uploaded Successfully");</script>';
   else
      echo '<script type="text/javascript">alert("Sorry Failed to Upload Your Resume");</script>';
} else {
?>

还有一个表单验证器来检查可能有用的空字段。

这是在 PHP 文档中

 var frmvalidator = new Validator("form1");
 frmvalidator.addValidation("fromname","req","Please enter your name");
 frmvalidator.addValidation("fromemail","req","Please enter your email");
 frmvalidator.addValidation("apply","req","Please enter the position you are applying for");
 frmvalidator.addValidation("file1","req","Please Upload Your resume");

如果有人建议我在 js 中添加文件类型验证器,我可以发布该代码。它很长,我相信它是一个常用的文件。

I would like to modify this resume uploader code I currently use to restrict the doc types of the applicant's attachment to doc, docX, rtf and PDF. I modified this from something I believe I found on SourceForge.net.

I would also like to redirect upon successful submission. Right now the page is blank upon completion. I can probably figure that out.

My PHP skills are not great, so I don't really know where to place the filetype validation code.

<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){

    $to="[email protected]";
    $subject= stripslashes($_POST['apply']);

   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

   $message= stripslashes($_POST['msg']);

   $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";

   foreach($_FILES as $userfile){
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];

      if (file_exists($tmp_name)){

         if(is_uploaded_file($tmp_name)){

            $file = fopen($tmp_name,'rb');
            $data = fread($file,filesize($tmp_name));
            fclose($file);

            $data = chunk_split(base64_encode($data));
         }

          $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n";
           }
   }

   $message.="--{$mime_boundary}--\n";
   if (@mail($to, $subject, $message, $headers))
     echo '<script type="text/javascript">alert("Resume Uploaded Successfully");</script>';
   else
      echo '<script type="text/javascript">alert("Sorry Failed to Upload Your Resume");</script>';
} else {
?>

There's also a form validator to check for empty fields that might be of use.

This is in the PHP document

 var frmvalidator = new Validator("form1");
 frmvalidator.addValidation("fromname","req","Please enter your name");
 frmvalidator.addValidation("fromemail","req","Please enter your email");
 frmvalidator.addValidation("apply","req","Please enter the position you are applying for");
 frmvalidator.addValidation("file1","req","Please Upload Your resume");

If anyone suggests that I add the filetype validator in the js, I can post that code. It is long and I believe it is a commonly used file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从来不烧饼 2024-12-12 11:38:37

服务器端验证在这里是不可避免的......

您应该在 *if(file_exists($tmp_name))* 之前验证文件类型,即覆盖整个 if 条件。

喜欢:

    if($userfile['type'] == in_array('filetype1','filetype2','filetype3')))
      {
        if(file_exists($tmp_name))
          {

        ........

          }
      } 

希望有帮助:)

Server-side validation is inevitable here ....

You should validate file-type just before *if(file_exists($tmp_name))* i.e. covering up this whole if-condition.

Like :

    if($userfile['type'] == in_array('filetype1','filetype2','filetype3')))
      {
        if(file_exists($tmp_name))
          {

        ........

          }
      } 

Hope it helps :)

傾城如夢未必闌珊 2024-12-12 11:38:37

可以通过检查上传文件的MIME-type来检查文件类型。遗憾的是,还没有一个可移植的解决方案来查找文件的 MIME 类型。您可以尝试使用 FileInfo 库:

$finfo = finfo_open(FILEINFO_MIME_TYPE);

foreach($_FILES as $userfile){
  $tmp_name = $userfile['tmp_name'];
  $type = $userfile['type'];
  $name = $userfile['name'];
  $size = $userfile['size'];

  $mime_type = finfo_file($finfo, $tmp_name);
  if($mime_type != "application/pdf" && 
     $mime_type != "application/msword" &&
     $mime_type != "application/vnd.openxmlformats-officedocument.wordprocessingml.document" &&
     $mime_type != "application/rtf" &&
     $mime_type != "text/rtf" &&
     $mime_type != "text/richtext")
        continue;
  ...
}
finfo_close($finfo);

另一种选择是使用 $type 变量而不是 $mime_type 进行比较。但这是不可靠的。

最后,作为最后的手段,您只需使用以下命令进行文件扩展名嗅探:

$filename = basename($tmp_name);            
$fileExt = strtolower(substr(strrchr($filename, "."), 1));

并将 $fileExt 与您想要的文件扩展名进行比较。 (docdocxrtfpdf

File types can be checked by checking the MIME-type of the file uploaded. And sadly, there hasn't been a portable solution into finding the MIME-type of a file. You can try using the FileInfo library:

$finfo = finfo_open(FILEINFO_MIME_TYPE);

foreach($_FILES as $userfile){
  $tmp_name = $userfile['tmp_name'];
  $type = $userfile['type'];
  $name = $userfile['name'];
  $size = $userfile['size'];

  $mime_type = finfo_file($finfo, $tmp_name);
  if($mime_type != "application/pdf" && 
     $mime_type != "application/msword" &&
     $mime_type != "application/vnd.openxmlformats-officedocument.wordprocessingml.document" &&
     $mime_type != "application/rtf" &&
     $mime_type != "text/rtf" &&
     $mime_type != "text/richtext")
        continue;
  ...
}
finfo_close($finfo);

Another option would be to use the $type variable for comparison instead of the $mime_type. But this is unreliable.

Finally, as a last resort, you just do a file extension sniffing using this:

$filename = basename($tmp_name);            
$fileExt = strtolower(substr(strrchr($filename, "."), 1));

And compare $fileExt with your desired file extensions. (doc, docx, rtf, and pdf)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文