2013-10-15 23 views
-1

我使用幫助聯機教程創建了帶有文件附件的PHP聯繫表單。但我想像用戶一樣只能上傳.txt .doc .docx文件格式,不會接受其他格式。僅製作附件文件格式.txt .doc .docx

<?php 
$from = "[email protected]"; 
$to = $_POST['email']; 
$subject =$_POST['subject']; 
$message = $_POST['body']; 

// Temporary paths of selected files 
$file1 = $_FILES['file1']['tmp_name']; 
$file2 = $_FILES['file2']['tmp_name']; 
$file3 = $_FILES['file3']['tmp_name']; 

// File names of selected files 
$filename1 = $_FILES['file1']['name']; 
$filename2 = $_FILES['file2']['name']; 
$filename3 = $_FILES['file3']['name']; 

// array of filenames to be as attachments 
$files = array($file1, $file2, $file3); 
$filenames = array($filename1, $filename2, $filename3); 

// include the from email in the headers 
$headers = "From: $from"; 

// boundary 
$time = md5(time()); 
$boundary = "==Multipart_Boundary_x{$time}x"; 

// headers used for send attachment with email 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\""; 

// multipart boundary 
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$boundary}\n"; 

// attach the attachments to the message 
for($x=0; $x<count($files); $x++){ 
    $file = fopen($files[$x],"r"); 
    $content = fread($file,filesize($files[$x])); 
    fclose($file); 
    $content = chunk_split(base64_encode($content)); 
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
     "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . 
     "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n"; 
    $message .= "--{$boundary}\n"; 
} 

// sending mail 
$sendmail = mail($to, $subject, $message, $headers); 

// verify if mail is sent or not 
if ($sendmail) { 
    echo "Sent successfully!"; 
} else { 
    echo "Error occurred. Try again!"; 
} 
?> 
+1

您是否試圖防止人們意外發送無助的文件,或者您是否試圖阻止惡意用戶傳播病毒? –

+0

[看到這個答案](http://stackoverflow.com/a/1529083/1415724)它會給你一個好主意,如何做到這一點。雖然我並不熱衷於使用W3School作爲參考,[您可以在這裏看到此頁](http://www.w3schools.com/php/php_php_file_upload.asp)在「限制上傳」部分 –

回答

1

使用$ _FILE的[「文件名」] [「型」]下不推薦用於文件類型檢查的類型參數是針對特定瀏覽器即不同的瀏覽器可以有不同的類型,因此,我們將嘗試提取格式從字符串功能

$filename=$_FILES["file1"]["name"];//get the name of the file 
$extension=strrchr($filename, ".");//extracting the extension 

if($extension=".txt" || $extension=".doc" || $extension=".docx") 
{ 
//send mail; 
} 
else 
{ 
//error; 
} 
+0

舊的,但這不起作用,if語句操作符也不正確。 – Tom

-1
function allowed_file(){ 

    //Add the allowed mime-type files to an 'allowed' array 
    $allowed = array('application/doc', 'application/txt', 'another/type'); 

    //Check uploaded file type is in the above array (therefore valid) 
    if(in_array($_FILES['file']['name'], $allowed)) 
     { 
     // Your code 
      } 

} 

參考:php file upload, how to restrict file upload type

+1

上。首先,此代碼不會運行。其次,如果它做到了,它會達到什麼目的? –

1

簡單,

$filename_array = explode(".",$filename); 
$ext = ".".$filename_array[count($filename_array)-1]; 
if($ext!==".txt" && $ext!==".doc" && $ext!==".docx"): 
    //Do bad extension code 
endif; 
//Do code that passed extension validation 

希望這有助於!

1
$allowed = array('.txt', '.doc', '.docx') 
$fileNames = array(); 

$filename1= $_FILES['file1']['name']; 
$ext1 = pathinfo($path, PATHINFO_EXTENSION); 

if(in_array($ext1, $allowed)){ 
    array_push($fileNames, $filename1); //repeat for 2, 3, etc... 
}