2011-09-19 43 views
0

我想修改这个简历上传器代码,我目前用它来限制申请人对doc,docX,rtf和PDF的附件的doc类型。我从我认为在SourceForge.net上找到的东西中修改了这一点。如何限制PHP表单邮件附件到某些文档类型?

我也想成功提交后重定向。目前这个页面完成后是空白的。我大概可以弄清楚。

我的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,我可以张贴代码。它很长,我相信它是一个常用的文件。

回答

0

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

你应该只*如果(file_exists($ tmp_name的值))*之前验证文件类型,即覆盖了这个整体,如果条件。

像:

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

     ........ 

      } 
     } 

希望它能帮助:)

0

文件类型可以通过检查上传的文件MIME-type进行检查。可悲的是,还没有找到文件的MIME-type的便携式解决方案。你可以尝试使用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。 (doc,docx,rtfpdf

+0

这会把Mac用户搞砸吗?或者他们的文件也有扩展名。 – Lazykins