【问题标题】:Email Form File Upload Returns Files Empty电子邮件表单文件上传返回文件为空
【发布时间】:2016-01-21 18:05:58
【问题描述】:

这是一个包含附件的简单 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));

无济于事。有人知道吗?

【问题讨论】:

    标签: php html forms input encoding


    【解决方案1】:

    玩了几个小时后,我终于弄明白了。我添加了一个调试功能,最初使用了 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!"; 
        } 
    }   
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 2019-09-29
      • 2011-09-02
      • 1970-01-01
      • 2012-01-02
      • 2011-02-12
      • 1970-01-01
      相关资源
      最近更新 更多