【问题标题】:Contact form with captcha - how to add file upload section? [duplicate]带有验证码的联系表格 - 如何添加文件上传部分? [复制]
【发布时间】:2020-05-12 12:53:50
【问题描述】:

我有一个基本的工作 PHP 联系表单,它使用静态常量图像作为验证码 - 我不太了解 PHP,但希望能够添加上传/发送附件部分,因此用户可以选择能够附加一个文件,该文件将附加到收件人收到的电子邮件中。我不知道如何做到这一点,也找不到谷歌搜索的帮助。有任何想法吗? 我有简单的工作上传代码 - 但似乎无法将两者结合起来。

联系表格 HTML

<form name="contactform" method="post" action="contact_send.php">
        <div class="contactdetails">
           <label for="name"> Name or Company Name<span class="mandatory"> *</span> </label> <input type="text" name="name" />  <br /> 
           <label for="telephone_number"> Telephone Number<span class="mandatory"> *</span> </label>  <input type="text" name="telephonenumber" /> <br />
           <label for="email_address"> Email Address </label> <input type="text" name="emailaddress" /> <br /> 

            <div class="commentarea">
              <p><label for="comments"> Comments </label> </p>
              <textarea name="comments" rows="auto" cols="auto"> </textarea> <br />
            </div>               

           <div id="typecont"> 
            <label for="type_see">Please type what you see in the box</label>   
              <div id="typecontimg"> 
                <img src="images/num.png" alt="captcha" style="width:100%;"/> <!-- style="width:65px;height:28px" -->
               </div>        
                  <div id="typein">
                   <input type="text" id="type_see" name="typesee"/>     
                  </div>
           </div>

               <div class="submitbutton">
                 <input type="submit" value="Send" name="submit" />
               </div>                 
        </div> 

和 PHP

<?php
        if(isset($_POST['submit'])) {

            $name = $_POST['name']; // required
            $telephone_number = $_POST['telephonenumber']; // required
            $email_address = $_POST['emailaddress'];                                        
            $comments = $_POST['comments'];                                                        
            $type_see = $_POST['typesee']; // required     

            if($name=='' || 
           $telephone_number=='' ||                                                                            
           $type_see != 8567 )    

            {  
                   die ('<p style="margin-top:40px;">You have not filled in all of the required fields or the captcha is wrong  - <br /> <br /> please go back and try again!</p>');  

           }

            else{
                $email_to = "info@companyname.com";
                $email_subject = "Contact from Company Name";
                $email_message = "Form details below.\n\n";

            function clean_string($string) {
              $bad = array("content-type","bcc:","to:","cc:","href");
              return str_replace($bad,"",$string);
            }

            $email_message .= "Name or Company Name: ".clean_string($name)."\n";
            $email_message .= "Telephone Number: ".clean_string($telephone_number)."\n";                
            $email_message .= "Email Address: ".clean_string($email_address)."\n";
            $email_message .= "Comments: ".clean_string($comments)."\n";


            // Create email headers
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "From: info@companyname.com" . "\r\n";
            $headers .= "Reply-To: info@companyname.com" . "\r\n";
            $headers .= "Content-type:text;charset=iso-8859-1" . "\r\n";
            mail($email_to, $email_subject, $email_message, $headers);                 
        }
        }

?>

我有一个简单的上传/发送表单:

HTML(相关部分)

    <div class="files">
<label>Attachment <input type="file" name="my_file" /></label>
</div>

以及用于上传的 PHP:

<?php   

if($_POST && isset($_FILES['my_file'])) {

$from_email         = 'inf@services.com'; //from mail, it is mandatory with some hosts
$recipient_email    = 'inf@services.com'; //recipient email (most cases it is your personal email)
$subject      = "Upload from company";

//Capture POST data from HTML form and Sanitize them, 
$sender_name    = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name
$reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header

/* //don't forget to validate empty fields 
if(strlen($sender_name)<1){
    die('Name is too short or empty!');
} 
*/

//Get uploaded file data
$file_tmp_name    = $_FILES['my_file']['tmp_name'];
$file_name        = $_FILES['my_file']['name'];
$file_size        = $_FILES['my_file']['size'];
$file_type        = $_FILES['my_file']['type'];
$file_error       = $_FILES['my_file']['error'];

if($file_error > 0)
{
    die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Upload error or no files uploaded. <br /> Please hit your back button and try again.</p>');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));

    $boundary = md5("sanwebe");
    //header
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "From:".$from_email."\r\n"; 
    $headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

    //plain text 
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    $body .= chunk_split(base64_encode($message)); 

    //attachment
    $body .= "--$boundary\r\n";
    $body .="Content-Type: $file_type; name=".$file_name."\r\n";
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
    $body .="Content-Transfer-Encoding: base64\r\n";
    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
    $body .= $encoded_content; 

$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{       
    die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Thank you for uploading your CV. <br /> We will be in touch shortly.</p>');
}else{
    die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Could not send mail! Please check your PHP mail configuration</p>');  
}

} ?>

【问题讨论】:

    标签: php forms attachment contacts captcha


    【解决方案1】:

    您可以使用&lt;input type="file" name="fl"&gt;,或随意命名并使用以下命令处理文件: $name = $_FILE['fl'].

    $_FILE[] 是一个关联数组,因此,要查看其内容,请使用print_r($name)

    您可以查看其临时位置等详细信息,并使用move_uploaded_file() 之类的功能将文件上传到服务器上的永久文件夹中。

    【讨论】:

    • 为什么这个问题被关闭了,为什么用户的 cmets 被删除了?我正在尝试通过此帮助论坛解决我遇到的客户问题!
    • @FizzWebDesign 我不知道。但是我的回答对你有帮助吗?
    • 抱歉,这是一般性评论 - 不是针对您的。我不怀疑,因为我用 HTML/CSS 编写代码并且可以调整 PHP,但对它了解不多。我认识现场格式,明天将使用它。谢谢
    • @FizzWebDesign 不客气!如果您觉得我的回答有帮助,可以将其标记为答案。如果您需要有关它的更多详细信息,请随时发表评论。玩得开心!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2013-03-06
    • 2012-09-07
    • 2013-11-11
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多