添加图像附件选项后无法使HTML / PHP表单正常工作

时间:2018-11-06 16:26:30

标签: php html

我刚刚在我的PHP表单中添加了一个附件。在添加附件之前,它运行良好,但是现在提交表单时,它进入浏览器错误页面/无法完成请求。

<?php
function post_captcha($user_response) {
    $fields_string = '';
    $fields = array(
        'secret' => 'captchasecret',
        'response' => $user_response
    );
    foreach($fields as $key=>$value)
    $fields_string .= $key . '=' . $value . '&';
    $fields_string = rtrim($fields_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result, true);
}

// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);

if (!$res['success']) {
    // What happens when the CAPTCHA wasn't checked
   print "<meta http-equiv=\"refresh\" content=\"0;URL=captcha-failed.html\">";
} else {
    // If CAPTCHA is successfully completed...

    // Paste mail function or whatever else you want to happen here!
    //echo '<br><p>CAPTCHA was completed successfully!</p><br>';
    //}


$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: {$Email}>" . "\r\n";

$EmailFrom = "mywebsite.com";
$EmailTo = "myemail.com";
$Subject = "TV Form";

$Name = "";
$Businessname="";
$Tel="";
$Email="mail@jarbcom.com";
$Business="";
$Locations="";
$Units="";
$DishCustomer="";
$Zip="";
$message="";

if (isset($_POST['businessname'])) {
    $BusinessName = Trim(stripslashes($_POST['businessname'])); 
}
if (isset($_POST['name'])) {
    $Name = Trim(stripslashes($_POST['name'])); 
}
if (isset($_POST['phone'])) {
$Tel = Trim(stripslashes($_POST['phone'])); 
}
if (isset($_POST['email'])) {
$Email = Trim(stripslashes($_POST['email'])); 
}
if (isset($_POST['business'])) {
$Business = Trim(stripslashes($_POST['business'])); 
}
if (isset($_POST['locations'])) {
$Locations = Trim(stripslashes($_POST['locations'])); 
}
if (isset($_POST['units'])) {
$Units = Trim(stripslashes($_POST['units'])); 
}
if (isset($_POST['dishcustomer'])) {
$DishCustomer = Trim(stripslashes($_POST['dishcustomer'])); 
}
if (isset($_POST['zip'])) {
$Zip = Trim(stripslashes($_POST['zip'])); 
}
if (isset($_POST['message'])) {
$message = Trim(stripslashes($_POST['message'])); 
}
if (isset($_POST['address'])) {
$Address = Trim(stripslashes($_POST['address'])); 
}
if (isset($_POST['tvsize'])) {
$Tvsize = Trim(stripslashes($_POST['tvsize'])); 
}
if (isset($_POST['tvnum'])) {
$Tvnum = Trim(stripslashes($_POST['tvnum'])); 
}
if (isset($_POST['boxes'])) {
$Boxes = Trim(stripslashes($_POST['boxes'])); 
}


// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "<br />\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "<br />\n";
$Body .= "Zip Code: ";
$Body .= $Zip;
$Body .= "<br />\n";
$Body .= "Phone: ";
$Body .= $Tel;
$Body .= "<br />\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "<br />\n";
$Body .= "Tv Size: ";
$Body .= $Tvsize;
$Body .= "<br />\n";
$Body .= "Number of TVs: ";
$Body .= $Tvnum;
$Body .= "<br />\n";
$Body .= "Where they want the boxes: ";
$Body .= $Boxes;
$Body .= "<br />\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "<br />\n";
$Body .= "File: " . clean_string($attachment) . "\n\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, $headers);

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
}
}
?>

这是我的HTML表单的重要部分(毫无意义,您无法阅读所有字段)

我确实有multipart / form-data

<form action="phpformcontact.php" method="POST" enctype="multipart/form-data">

这是我添加的附件的字段

<label for="tvimage" class="sr-only control-label">Image to upload</label>
              <input placeholder="Upload Image" type="file" name="attachment" id="file" class="form-control input-lg"><span class="help-block text-danger"></span>

在添加附件代码之前,该表单可以很好地工作。任何帮助,将不胜感激。另外,如果不是png / jpeg / pdf / etc,是否有一种简单的方法可以添加过滤器以拒绝附件。否则,将病毒上传到您的服务器真的不容易吗?

非常感谢您的帮助。

编辑:根据要求,这是完整的HTML表单

 <form action="phpformcontact.php" method="POST" enctype="multipart/form-data">


           <div class="col-md-4">
                          <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="name" class="sr-only control-label">Name*</label>
              <input id="name" name="name" type="text" placeholder="Full Name&#42" required="" data-validation-required-message="Please enter your full name" class="form-control input-lg"><span class="help-block text-danger"></span>
            </div>
          </div>
          </div>

          <div class="col-md-4">
                          <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="address" class="sr-only control-label">Address*</label>
              <input id="address" name="address" type="text" placeholder="Address&#42" required="" data-validation-required-message="Please enter your address" class="form-control input-lg"><span class="help-block text-danger"></span>
            </div>
          </div>
          </div>

          <div class="col-md-4">
                          <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="zip" class="sr-only control-label">Zip Code</label>
              <input id="zip" name="zip" type="text" placeholder="Business Zip Code&#42" required="" data-validation-required-message="Please enter your business zip code" class="form-control input-lg"><span class="help-block text-danger"></span>
            </div>
          </div>
          </div>

          <div class="col-md-4">

               <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="phone" class="sr-only control-label">Phone*</label>
              <input id="phone" name="phone" type="tel" placeholder="Phone&#42" required="" data-validation-required-message="Please enter phone number" class="form-control input-lg"><span class="help-block text-danger"></span>
            </div>
          </div>

          </div>

          <div class="col-md-4">
          <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="email" class="sr-only control-label">Email*</label>
              <input id="email" name="email" type="email" placeholder="Email&#42" required="" data-validation-required-message="Please enter email" class="form-control input-lg"><span class="help-block text-danger"></span>
            </div>
          </div>
          </div>

          <div class="col-md-4">
          <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="tvsize" class="sr-only control-label">TV Size*</label>
              <input id="tvsize" name="tvsize" type="text" placeholder="TV Size&#42" required="" data-validation-required-message="Please enter the size of the TV(s)" class="form-control input-lg"><span class="help-block text-danger"></span>
            </div>
          </div>
          </div>

          <div class="col-md-4">
            <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="tvnum" class="sr-only control-label">Number of TVs*</label>
              <input id="tvnum" name="tvnum" type="text" placeholder="Numbers of TVs&#42" required="" data-validation-required-message="Please enter the number of TVs" class="form-control input-lg"><span class="help-block text-danger"></span>
            </div>
          </div>
          </div>




          <div class="col-md-4">
                          <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="boxes" class="sr-only control-label">Where do you want the boxes?</label>
            <!--  <input id="business" type="text" placeholder="Type of Business" required="" data-validation-required-message="Please enter your type of business" class="form-control input-lg"><span class="help-block text-danger"></span> -->
              <select name="boxes" class="form-control input-lg"><span class="help-block text-danger">
<option value="">Where do you want the boxes?</option>
<option value="same-room" onclick="changeValue(&#39;Same Room&#39;);">Same Room</option>
<option value="different-location" onclick="changeValue(&#39;Different Location&#39;);">Different Location</option>
<option value="idk" onclick="changeValue(&#39;Don't Know&#39;);">Don't Know</option></select></div></div></div>                                

要上传的图片

       <div class="col-md-12">
          <div class="control-group">
            <div class="form-group floating-label-form-group controls">
              <label for="message" class="sr-only control-label">Details</label>
              <textarea name="message" id="message" rows="2" placeholder="Any Other Details" class="form-control input-lg"></textarea><span class="help-block text-danger"></span>

            </div>
          </div>
          <div id="success"></div>
          <button type="submit" class="btn btn-dark btn-lg text-center btn-danger">Send</button>
          </div>

        </form>

0 个答案:

没有答案