2015-01-15 74 views
1

我有一個腳本PHP表單發送到郵件與文件附件,但它沒有一個函數來有效的文件大小和文件擴展名。PHP表單發送到郵件與文件附件

下面

### Attachment Preparation ### 

$file_attached = false; 
if(isset($_FILES['file_attach'])) //check uploaded file 
{ 
    //get file details we need 
    $file_tmp_name = $_FILES['file_attach']['tmp_name']; 
    $file_name  = $_FILES['file_attach']['name']; 
    $file_size  = $_FILES['file_attach']['size']; 
    $file_type  = $_FILES['file_attach']['type']; 
    $file_error  = $_FILES['file_attach']['error']; 

    //exit script and output error if we encounter any 
    if($file_error>0) 
    { 
     $mymsg = array( 
     1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
     2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
     3=>"The uploaded file was only partially uploaded", 
     4=>"No file was uploaded", 
     6=>"Missing a temporary folder"); 

     $output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error])); 
     die($output); 
    } 

    //read from the uploaded file & base64_encode content for the mail 
    $handle = fopen($file_tmp_name, "r"); 
    $content = fread($handle, $file_size); 
    fclose($handle); 
    $encoded_content = chunk_split(base64_encode($content)); 
    //now we know we have the file for attachment, set $file_attached to true 
    $file_attached = true; 
} 

if($file_attached) //continue if we have the file 
{ 
    # Mail headers should work with most clients 
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers = "X-Mailer: PHP/" . phpversion()."\r\n"; 
    $headers .= "From: ".$from_email."\r\n"; 
    $headers .= "Subject: ".$subject."\r\n"; 
    $headers .= "Reply-To: ".$user_email."" . "\r\n"; 
    $headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n"; 

    $headers .= "--".md5('boundary1')."\r\n"; 
    $headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n"; 

    $headers .= "--".md5('boundary2')."\r\n"; 
    $headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n"; 
    $headers .= $message_body."\r\n\r\n"; 

PHP代碼,這是PHP表單代碼

<label> 
    <span>Attachment</span> 
    <input type="file" name="file_attach" class="input-field" /> 
</label> 
<label> 
    <span>&nbsp;</span><input type="submit" id="submit_btn" value="Submit" /> 
</label> 

我不想使用jQuery我想用PHP來做到這一點。

回答

1

你會使用這樣的驗證文件擴展名:

if($_FILES["file_attach"]["type"] == "image/jpeg") { //... 

替換「圖像/ JPEG」與任何的$_FILE['file_attach']['type']值是你希望它有實際的文件類型。

作爲驗證的大小,你會做這樣的事情:

if($_FILE["file_attach"]["size"] > 1000000) { //... 

最終,這可能是它的工作到現有腳本的最佳方式:

//[...] 


$file_error  = $_FILES['file_attach']['error']; 


if($_FILES["file"]["type"] != "image/jpeg"){ 
    $file_error = 7; 
}elseif($_FILES["file_attach"]["size"] > 1000000) { // = 1MB 
    $file_error = 8; 
} 


    //exit script and output error if we encounter any 
    if($file_error>0) 
    { 
     $mymsg = array( 
     1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
     2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
     3=>"The uploaded file was only partially uploaded", 
     4=>"No file was uploaded", 
     6=>"Missing a temporary folder" 
     7=>"We do not accept files of that type.", 
     8=>"Files cannot be larger than 1MB"); 

     $output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error])); 
     die($output); 
    } 
//[...] 
+0

感謝你的答案,但我面臨一個更多的麻煩,像一個錯誤,我已經把擴展名爲(jpeg),當我嘗試上傳任何文件它只顯示我的錯誤與本文(空) –

+0

你看到7和8的值我添加到你的'$ mymsg'數組中?我不能幫你,如果你不能更具體的錯誤 - –

+0

這是我的一些額外的點等錯誤,現在它的工作非常好,它顯示我錯誤,當我嘗試上傳額外的大小文件或也顯示我錯誤,當我嘗試上傳無效的文件擴展名。現在只有一件事我想知道我需要放置文件擴展名?我想只允許像(JPG,GIF,PNG)等圖像文件,我需要把這些擴展名,也是什麼格式? –