带验证码的联系表格-如何添加文件上传部分? [重复]

问题描述 投票:-1回答:1
我有一个基本的有效PHP联系人表单,该表单使用静态常量图像作为验证码-我不太了解PHP,但希望能够添加一个上载/发送附件部分,因此用户可以选择附加一个文件,该文件将附加到收件人收到的电子邮件中。我不知道如何执行此操作,也无法在Google上找到帮助。有任何想法吗?我有简单的工作上传代码-但似乎无法将两者结合起来。

联系表格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 = "[email protected]"; $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: [email protected]" . "\r\n"; $headers .= "Reply-To: [email protected]" . "\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 = '[email protected]'; //from mail, it is mandatory with some hosts $recipient_email = '[email protected]'; //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个回答
-1
投票
您可以使用<input type="file" name="fl">,也可以根据需要命名,并使用以下方法处理文件:$name = $_FILE['fl']

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

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

© www.soinside.com 2019 - 2024. All rights reserved.