php 发送带有附件的电子邮件表单,验证大小和扩展名

php send email form with attachment(s) validate the size and extensions

我编写了这段代码,现在我运行头靠墙了。

似乎代码无法验证文件的大小和扩展名!

我确实收到了文件和电子邮件。有人可以解释一下为什么我的验证失败吗?

    <?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 = "my@email.dk";
$subject = "Besked fra hjemmesiden";
$mailheader = "Fra: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: /thank_you.shtml')
?>

HTML 格式代码:

    <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="din@mail.dk" id="email" required="required" /><span class="red">&#42;</span>
<br />
<label for="phone">Telefon</label>
<input type="text" name="phone" placeholder="012345678" 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,等等,并能够添加更多文件?

您的代码没有特别的问题。我唯一注意到的是您没有在任何地方初始化 $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 你可以找出这个数组在 PHP 中的样子,并相应地验证每个文件。

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

调试后,请记住删除 header 函数之前的所有 echoprint_r 函数调用。否则您将收到以下错误:

Warning: Cannot modify header information - headers already sent