电子邮件表单文件上载返回文件空

时间:2016-01-21 18:05:58

标签: php html forms input encoding

这是一个简单的php电子邮件表单,其中包含附件。该表单正常工作,除了上传文件为他们上传的附件,但没有内容。我认为这意味着正在为附件上传的文件未被正确解码。这是代码:

    <?php 
$action = $_REQUEST['action']; 
if ($action=="")    /* display the contact form */ 
{
?>
    <form action="" enctype="multipart/form-data" method="post">
        <p>
            <label>
                <input type="checkbox" name="locate" id="locate ">
                <strong>Locate an Installer</strong>
            </label>
            <strong>
                <label>
                    <input type="checkbox" name="become" id="become">Become an Installer
                </label>
            </strong>
        </p>
        <div class="left">
            Name:
            <div>
                <input name="name" type="text" id="name" size="40" />
            </div>
        </div>
        <div class="left">
            Email:
            <div>
                <input name="mailfrom" type="text" size="40" />
            </div>
        </div>
        <div class="left">
            Street:
            <div>
                <input name="street" type="text" id="street" size="40" />
            </div>
        </div>
        <div class="left">
            City:
            <div>
                <input name="city" type="text" id="city" size="40" />
            </div>
        </div>
        <div class="left">
            State:
            <div>
                <input name="state" type="text" id="state" size="10" /> Zip:
                <input name="zip" type="text" id="zip" size="15" />
            </div>
        </div>
        <div class="left">
            Country:
            <div>
                <input name="country" type="text" id="country" size="40" />
            </div>
        </div>
        <div class="left">
            Phone:
            <div>
                <input name="phone" type="text" id="phone" size="40" />
            </div>
        </div>
        <p>The fields above are REQUIRED<br>
            <strong>
                How did you hear about AirKrete<span style="font-size:11.0pt; ">®</span>? <br>Please make a selection.
                <span id="sprycheckbox1">
                    <span class="checkboxRequiredMsg"></span>
                </span>
            </strong>
        </p>
        <div class="radiox"><label><input type="radio" name="Connection" value="friend" id="Connection_0">Friend</label></div>
        <div class="radiox"><label><input type="radio" name="Connection" value="jobspec" id="Connection_1">Job Specification</label></div>
        <div class="radiox"><label><input type="radio" name="Connection" value="hgtv" id="Connection_2">HGTV Promotion</label></div>
        <div class="radiox"><label><input type="radio" name="Connection" value="web" id="Connection_3">Web Browsing</label></div>
        <div class="radiox"><label><input type="radio" name="Connection" value="radio" id="Connection_4">Radio</label></div>
        <div class="radiox"><label><input type="radio" name="Connection" value="tv" id="Connection_5">TV</label></div>
        <div class="radiox"><label><input type="radio" name="Connection" value="other" id="Connection_6">Other</label></div></p>
        <p class="clear-fix">&nbsp;</p>
        <div><p><strong>Comments:</strong><br></p></div>
        <p><textarea name="Comments" cols="43" rows="10" id="Comments"></textarea></p>
        <p><label>If you have Attachments....</label></p>
        <p><input name="file_attachment" type="file" size="30" /></p>
        <p>When completed please click Send Email Button</p>
        <input type="submit" value="Send Email" name="action" />
        <input type="hidden" name="redirect" value="http://www.airkrete.com/airkrete_thankyou.php" /><p>Thank You</p>
    </form>
<?php 
} 
else                /* send the submitted data */ 
{
    $locate = isset($_REQUEST['locate']) ? $_REQUEST['locate'] : "" ;
    $become = isset($_REQUEST['become']) ? $_REQUEST['become'] : "" ;
    $mailfrom = $_REQUEST['mailfrom'];
    $street = $_REQUEST['street'];
    $city = $_REQUEST['city'];
    $state = $_REQUEST['state'];
    $zip = $_REQUEST['zip'];
    $country = $_REQUEST['country'];
    $phone = $_REQUEST['phone'];
    $connection = $_REQUEST['Connection'];
    $comments = $_REQUEST['Comments'];
    $name = $_REQUEST['name']; 
$filename = "uploads/".basename($_FILES['file_attachment']['name']);
move_uploaded_file($_FILES["file_attachment"]["name"], $filename);
$file_size = $_FILES["file_attachment"]["size"]/1024;//size in KBs
$handle = fopen($filename, "a+");
$content = fread($handle, $file_size);
fclose($handle);
$content= chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));

    if ((($locate == "") && ($become == "")) || ($mailfrom == "") || ($street == "") || ($city == "") || ($state == "") || ($zip == "") || ($country == "") || ($phone == "") || ($name == ""))
    { 
        echo "Not all required forms were filled out, please fill <a href=\"\">the form</a> again."; 
    } 
    else
    {   
        if(($locate == "") && ($comments != "") && ($connection != ""))
        {      
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
            $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($become == "") && ($comments != "") && ($connection != ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($locate == "") && ($comments != "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($locate == "") && ($comments == "") && ($connection != ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
        }
        else if(($become == "") && ($comments != "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.".\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($become == "") && ($comments == "") && ($connection != ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
        }
        else if(($locate == "") && ($comments == "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone.".\r\n";
        }
        else if(($become == "") && ($comments == "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.".\r\n";
        }
        else
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about become an installer and would also like to locate one. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        if($filename != ""){
    $message .= "Content-Type: application/octet-stream; name=\"http://airkrete.com/".$filename."\"\r\n"; // use different content types here
    $message .= "Content-Transfer-Encoding: base64\r\n";
    $message .= "Content-Disposition: attachment; filename=\"http://airkrete.com/".$filename."\"\r\n\r\n";
    $message .= $content."\r\n\r\n";
        }
        $from = "From: $name<$mailfrom>\r\nReturn-path: $mailfrom"; 
        $subject = "Someone's interested in either finding an installer or becoming one!"; 
        mail("joseph.roberts@usa.com", $subject, $message, $from); 
        echo "Email sent!"; 
    } 
}   
?>

非常感谢任何帮助。我可能完全错误地认为为什么上传的文件是空白的,但编码是一种暗示。

我尝试删除以下行:

    $content= chunk_split(base64_encode($content));

无济于事。有人有线索吗?

1 个答案:

答案 0 :(得分:0)

经过几个小时的玩弄,我终于明白了。我添加了一个调试功能,最初使用了tmp_name。虽然我不知道我改变了什么让它现在发挥作用,但这里有改变它的工作代码。

        <?php 
} 
else                /* send the submitted data */ 
{
    $locate = isset($_REQUEST['locate']) ? $_REQUEST['locate'] : "" ;
    $become = isset($_REQUEST['become']) ? $_REQUEST['become'] : "" ;
    $mailfrom = $_REQUEST['mailfrom'];
    $street = $_REQUEST['street'];
    $city = $_REQUEST['city'];
    $state = $_REQUEST['state'];
    $zip = $_REQUEST['zip'];
    $country = $_REQUEST['country'];
    $phone = $_REQUEST['phone'];
    $connection = $_REQUEST['Connection'];
    $comments = $_REQUEST['Comments'];
    $name = $_REQUEST['name']; 
$uid = md5(uniqid(time()));


  if(isset($_FILES['file_attachment'])){
  $errors= array();
  $file_name = $_FILES['file_attachment']['name'];
  $file_size =$_FILES['file_attachment']['size'];
  $file_tmp =$_FILES['file_attachment']['tmp_name'];
  $file_type=$_FILES['file_attachment']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['file_attachment']['name'])));

  $expensions= array("jpeg","jpg","png");

  if(in_array($file_ext,$expensions)=== false){
     $errors[]="Extension not allowed, please choose a JPEG or PNG file. ";
  }

  if($file_size > 52428800){
     $errors[]='File size must be under 50 MB. ';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"uploads/".$file_name);
     echo "Success! ";
  }else{
     print_r($errors);
  }
   }

    if ((($locate == "") && ($become == "")) || ($mailfrom == "") || ($street == "") || ($city == "") || ($state == "") || ($zip == "") || ($country == "") || ($phone == "") || ($name == ""))
    { 
        echo "Not all required forms were filled out, please fill <a href=\"\">the form</a> again."; 
    } 
    else
    {   
        if(($locate == "") && ($comments != "") && ($connection != ""))
        {      
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
            $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($become == "") && ($comments != "") && ($connection != ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($locate == "") && ($comments != "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($locate == "") && ($comments == "") && ($connection != ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
        }
        else if(($become == "") && ($comments != "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.".\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        else if(($become == "") && ($comments == "") && ($connection != ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
        }
        else if(($locate == "") && ($comments == "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about becoming an installer. My phone number is ".$phone.".\r\n";
        }
        else if(($become == "") && ($comments == "") && ($connection == ""))
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about locating an installer. My phone number is ".$phone.".\r\n";
        }
        else
        {
            $message = "FROM: ".$name." <".$mailfrom.">\r\n";
            $message .= "--".$uid."\r\n";
            $message .= "Located: ".$street." ".$city." ".$state." ".$zip." ".$country."\r\n\r\n";
    $message .= "I would like to inquire about become an installer and would also like to locate one. My phone number is ".$phone.". I heard about you from: ".$connection."\r\n";
            $message .= "I also left the following comment:\r\n".$comments."\r\n\r\n";
        }
        if($file_name != ""){
    $message .= "Content-Type: application/octet-stream; name=\"http://airkrete.com/uploads/".$file_name."\"\r\n"; // use different content types here
    $message .= "Content-Transfer-Encoding: base64\r\n";
    $message .= "Content-Disposition: attachment; filename=\"http://airkrete.com/uploads/".$file_name."\"\r\n\r\n";
    $message .= $content."\r\n\r\n";
        }
        $from = "From: $name<$mailfrom>\r\nReturn-path: $mailfrom"; 
        $subject = "Someone's interested in either finding an installer or becoming one!"; 
        mail("info@airkrete.com", $subject, $message, $from); 
        echo "Email sent!"; 
    } 
}   
?>