2016-06-12 60 views
0

我已經制作了這段代碼,現在我的頭靠牆了。PHP發送帶附件的電子郵件表格驗證尺寸和擴展

看來代碼無法兼顧文件的大小或擴展名!

我收到文件和電子郵件。有人能解釋我爲什麼驗證失敗嗎?

<?php 
//Get the uploaded file information 
$name_of_uploaded_file = 
    basename($_FILES['uploaded_file']['name']); 

//get the file extension of the file 
$type_of_uploaded_file = 
    substr($name_of_uploaded_file, 
    strrpos($name_of_uploaded_file, '.') + 1); 

$size_of_uploaded_file = 
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs 

//Settings 
$max_allowed_file_size = 2000; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "pdf"); 
$upload_folder = '../receivedfiles/'; 

//Validations 
if($size_of_uploaded_file > $max_allowed_file_size) 
{ 
    $errors .= "\n Fejl: Filen er for stor"; 
} 

//------ Validate the file extension ----- 
$allowed_ext = false; 
for($i=0; $i<sizeof($allowed_extensions); $i++) 
{ 
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) 
    { 
    $allowed_ext = true; 
    } 
} 

if(!$allowed_ext) 
{ 
    $errors .= "\n The uploaded file is not supported file type. ". 
    " Send venligst filer af følgende type: ".implode(',',$allowed_extensions); 
} 

//copy the temp. uploaded file to uploads folder 
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; 
$tmp_path = $_FILES["uploaded_file"]["tmp_name"]; 

if(is_uploaded_file($tmp_path)) 
{ 
    if(!copy($tmp_path,$path_of_uploaded_file)) 
    { 
    $errors .= '\n error while copying the uploaded file'; 
    } 
} 

$name = $_POST['name']; 
$email = $_POST['email']; 
$phone = $_POST['phone']; 
$call = $_POST['call']; 
$company = $_POST['company']; 
$type = $_POST['type']; 
$adress = $_POST['adress']; 
$hesteid = $_POST['hesteid']; 
$hestenavn = $_POST['hestenavn']; 
$message = $_POST['message']; 
$areacode = $_POST['areacode']; 
$land = $_POST['land']; 
$formcontent=" Fra: $company \n Navn: $name \n Adresse: $adress , $areacode \n Land: $land \n Telefon: $phone \n Ringes op: $call \n Type: $type \n Hoppens navn og ID: $hestenavn , $hesteid \n Besked: \n $message \n Vedhæftede filer: \n $path_of_uploaded_file"; 
$recipient = "[email protected]"; 
$subject = "Besked fra hjemmesiden"; 
$mailheader = "Fra: $email \r\n"; 
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); 
header('Location: /thank_you.shtml') 
?> 

HTML formcode:

<form class="contactform" action="php/mail.php" method="post" enctype="multipart/form-data"> 
<label for="company">Firma</label> 
<input type="text" name="company" id="company" /> 
<label for="name">Navn</label> 
<input type="text" name="name" placeholder="Dit navn" id="name" required="required" /><span class="red">&#42;</span> 
<br /> 
<label for="email">Email</label> 
<input type="email" name="email" placeholder="[email protected]" id="email" required="required" /><span class="red">&#42;</span> 
<br /> 
<label for="phone">Telefon</label> 
<input type="text" name="phone" placeholder="" id="phone" required="required" /><span class="red">&#42;</span> 
<br /> 
<label for="adress">Adresse</label> 
<input type="text" name="adress" placeholder="Din adresse" id="adress" /> 
<br /> 
<label for="areacode">Postnummer og By</label> 
<input type="text" name="areacode" placeholder="Postnummer og By" id="areacode" /> 
<br /> 
<label for="land">Land</label> 
<input type="text" name="land" placeholder="Land" id="land" /> 
<br /> 
<br /> 
<label for="call">Vil De ringes op?<span class="red">&#42;</span></label> 
<table class="callme"> 
<tr> 
<td class="callmetext"><p>Ja</p></td> 
<td class="callmecheck"><input type="radio" value="Ja" name="call" id="call" required="required" /></td> 
</tr> 
<tr> 
<td class="callmetext"><p>Nej</p></td> 
<td class="callmecheck"><input type="radio" value="Nej" name="call" /></td> 
</tr> 
</table> 
<br /> 
<label for="type">Emne</label> 
<select name="type" size="1" id="type" required="required"> 
<option value="">Vælg fra listen</option> 
<option value="Bestille dek&#230;ning">Bestille bedækning</option> 
<option value="Bestille brochure">Bestille brochure</option> 
<option value="Information om en hingst">Information om en hingst</option> 
<option value="Information om stutteriet">Information om stutteriet</option> 
<option value="Information om salgsheste">Information om salgsheste</option> 
<option value="Information om Afkomsformidling">Information om afkomsformidling</option> 
<option value="Information om samarbejdspartnere">Information om vore samarbejdspartner</option> 
<option value="Andet">Andet</option> 
</select><span class="red">&#42;</span> 
<br /> 
<label for="hesteid">Hoppens ID</label> 
<input type="text" name="hesteid" placeholder="208333DW..." id="hesteid" /> 
<br /> 
<label for="hestenavn">Hoppens navn</label> 
<input type="text" name="hestenavn" placeholder="Hoppens navn..." id="hestenavn" /> 
<br /> 
<br /> 
<label for="message">Din besked<span class="red">&#42;</span></label> 
<textarea name="message" rows="6" cols="20" placeholder="Skriv din besked her..." id="message" required="required"></textarea> 
<br /> 
<br /> 
<label for="uploaded_file">Vælg en fil:</label> 
<input type="file" name="uploaded_file"> 
<br /> 
<br /> 
<div class="center"> 
<label for="captcha">&#32;</label> 
<div id="captcha" class="g-recaptcha" data-callback="recaptchaCallback" data-sitekey="6LfnbiATAAAAAOBV8B7qaPGzfpyjdahePpyGhLjj"></div> 
</div> 
<br /> 
<br /> 
<div class="center"> 
<label for="submitBtn">&#32;</label> 
<input class="contactbtn" type="submit" value="Send besked" id="submitBtn" disabled /> 
</div> 
</form> 

獎金的問題:我將能夠複製此代碼,並改變「uploaded_file」到「uploaded_file2」,和3,等等,並能增加幾個更多文件?

+0

我沒有看到此代碼的任何問題。 'print_r($ _ FILES)'產生什麼樣的輸出? – Midas

+0

對不起,我不熟悉這個命令。我怎樣才能插入這個? –

+0

它打印'$ _FILES'數組的內容。只需將它插入代碼中的任何位置。 – Midas

回答

0

你的代碼沒有特別的問題。我唯一注意到的是,您不是在發送電子郵件之前在任何地方初始化$error,或者檢查其內容。

而且,這個代碼整塊:

$allowed_ext = false; 
for ($i = 0; $i < sizeof($allowed_extensions); $i++) 
{ 
    if (strcasecmp($allowed_extensions[$i], $type_of_uploaded_file) == 0) 
    { 
     $allowed_ext = true; 
    } 
} 

...可以簡化爲:

$allowed_ext = in_array(strtolower($type_of_uploaded_file), $allowed_extensions); 

並回答了獎金問題:要實現多文件上傳,你可以使用您input名稱的數組:

<input type="file" name="uploaded_file[]"> 
<input type="file" name="uploaded_file[]"> 
<input type="file" name="uploaded_file[]"> 

使用print_r你可以在n找出這個數組在PHP中的樣子,並相應地驗證每個文件。

echo '<pre>'; print_r($_FILES); echo '</pre>'; 

調試完畢後,記得刪除任何echoprint_r函數調用的函數header之前到來。否則,您將收到以下錯誤:

Warning: Cannot modify header information - headers already sent 
+0

感謝您的回答。 我不確定要檢查什麼。對於我希望在腳本上傳到服務器文件夾的同時發送文件的路徑和名稱的電子郵件。我看不出爲什麼應該初始化錯誤?此外,我相信代碼應該出來,並告訴如果文件超過最大限制或有一個錯誤的擴展名? 感謝您對腳本的重寫:) 同樣感謝您對multiupload的提示!我會繼續:) –

+0

@SimonJensen啓用[錯誤報告](http://php.net/manual/en/function.error-reporting.php),你就會知道爲什麼你需要初始化'$ error '變量。它會告訴你在變量被初始化之前你不能追加('。=')。當錯誤報告被禁用時,PHP將默默允許修改未初始化的變量,但這通常是不好的做法。此外,除非您告訴PHP打印驗證錯誤,否則代碼不會告訴您有關驗證的任何內容。 :) – Midas

+0

我把所有'$ errors'都改成了〜'。這有很大幫助。下一步是使其彈出:) 我發現我無法訪問php.ini,所以我無法呈現它。但是這對麻煩有很大幫助。我相信我的新需求超過了這個線程。感謝一堆:) –