2011-09-19 76 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