Php 如何将文件上载添加到现有联系人表单

Php 如何将文件上载添加到现有联系人表单,php,html,forms,email,Php,Html,Forms,Email,我有一个现有的html联系人表单,但我想添加上传照片作为附件发送的功能。如何将其添加到我的php脚本中。我已经在html form name=datafile中添加了文件上载字段,并且还设置了属性enctype=“multipart/form data” 这是我的HTML部分 <form method="post" action="sendMail.php" enctype="multipart/form-data" id="contactform">

我有一个现有的html联系人表单,但我想添加上传照片作为附件发送的功能。如何将其添加到我的php脚本中。我已经在html form name=datafile中添加了文件上载字段,并且还设置了属性enctype=“multipart/form data”


这是我的HTML部分

<form method="post" action="sendMail.php" enctype="multipart/form-data" id="contactform">

                    <div class="response">&nbsp;</div>

                    <p ><label for="contactname">What is your full legal Name?<span>*</span></label>
                        <input id="contactname" type="text" value="" name="contactname" placeholder="Full Name" class="textflied">
                        <i class="icon fa fa-user"></i></p>

                        <p><label for="email" >What is the best email address for us to contact you on?<span>*</span></label>
                            <input id="email" type="text" value="" name="email" placeholder="Email Address" class="textflied">
                            <i class="icon fa fa-envelope"></i></p>

                            <p><label for="subject" >Now tell us the best phone number to reach you at:<span>*</span></label>
                                <input id="subject"  type="text" value="" name="subject" placeholder="Your phone number including area code" class="textflied">
                                <i class="icon fa fa-phone  "></i></p>

                                <p><label for="message" >In a short paragraph how would you discribe your self?<span>*</span></label>
                                    <textarea id="message" type="text" name="message" value="" placeholder="Your short paragraph" rows="8" class="texttextarea"></textarea>
                                     <i class="icon fa fa-comments"></i></p>

                                     <label for="datafile" >Finally lets match the name with a face, upload your most recent photo<span>*</span></label>
                                     <input type="file" name="datafile" size="40">
                                     <br>
                                     <br>

                                    <p>
                                        <button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-disabled">SEND message</button>
                                    </p>
                                </form>

您的法定全名是什么*

我们与您联系的最佳电子邮件地址是什么*

现在告诉我们联系您的最佳电话号码:*

在一小段中,你会如何描述自己*

最后让我们把名字和脸匹配起来,上传你最近的照片*

发送消息


请添加此代码并检查它是否对您有帮助

if(trim($_FILES['datafile']['name'])==''){
 $hasError = true;
}
else {
    $target='upload/';/*give the directory name wfere you want to save it*/
move_uploaded_file($_FILES['datafile']['tmp_name'],$target);
}








  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
    <?php
$yourEmailAddress = '123@123.com'; //Put your own email address here.
if(trim($_FILES['datafile']['name'])==''){
     $hasError = true;
    }
    else {
        $target='upload/';/*give the directory name wfere you want to save it*/
    move_uploaded_file($_FILES['datafile']['tmp_name'],$target);
}
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
    $hasError = true;
} else {
    $subject = trim($_POST['subject']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!$hasError) {
    $emailTo = $yourEmailAddress;
    $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $comments";
    $headers = 'From: Interion Template <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
    mail($emailTo, $subject, $body, $headers);
    echo'<div id="success" class="sent success"><p><strong>Email Successfully Sent!</strong><br>Thanks for contacting Us. Your email was successfully sent and we \'ll be in touch with you soon.</p></div>';
} else { //If errors are found
    echo '<p class="error">Please check if you\'ve filled all the fields with valid information and try again. Thank you.</p>';
} 

 ?>
</head>

<body>
<form method="post" action="" enctype="multipart/form-data" id="contactform">
  <div class="response">&nbsp;</div>
  <p >
    <label for="contactname">What is your full legal Name?<span>*</span></label>
    <input id="contactname" type="text" value="" name="contactname" placeholder="Full Name" class="textflied">
    <i class="icon fa fa-user"></i></p>
  <p>
    <label for="email" >What is the best email address for us to contact you on?<span>*</span></label>
    <input id="email" type="text" value="" name="email" placeholder="Email Address" class="textflied">
    <i class="icon fa fa-envelope"></i></p>
  <p>
    <label for="subject" >Now tell us the best phone number to reach you at:<span>*</span></label>
    <input id="subject"  type="text" value="" name="subject" placeholder="Your phone number including area code" class="textflied">
    <i class="icon fa fa-phone  "></i></p>
  <p>
    <label for="message" >In a short paragraph how would you discribe your self?<span>*</span></label>
    <textarea id="message" type="text" name="message" value="" placeholder="Your short paragraph" rows="8" class="texttextarea"></textarea>
    <i class="icon fa fa-comments"></i></p>
  <label for="datafile" >Finally lets match the name with a face, upload your most recent photo<span>*</span></label>
  <input type="file" name="datafile" size="40">
  <br>
  <br>
  <p>
    <button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-disabled">SEND message</button>
  </p>
</form>
</body>
</html>
if(修剪($\u文件['datafile']['name'])=''){
$hasError=true;
}
否则{
$target='upload/';/*在保存之前,请给出目录名*/
移动上传的文件($文件['datafile']['tmp\U名称],$target);
}
无标题文件

你的法定全名是什么*

我们与您联系的最佳电子邮件地址是什么*

现在告诉我们联系您的最佳电话号码:*

在一小段中,你会如何描述自己*

最后让我们把名字和脸匹配起来,上传你最近的照片*

发送消息


你的html部件在哪里?你尝试了什么,出现了什么错误?“我总是出错”在任何时候、任何地方都没有帮助过任何人。这是什么意思?什么错误?尝试什么修改时?您应该发布您的尝试,并明确指出您遇到的具体错误,以便我们提供帮助。“错误”请检查您是否已用有效信息填写所有字段,然后重试。谢谢。

';这并不能回答我的问题,我已经有了表单和php,我如何才能将上传部分添加到我现有的代码中谢谢,我注意到我必须提供目录,有没有一种方法可以简单地将其附加到电子邮件中?是的,你可以创建一个url并将其发送给用户我在测试时遇到这个错误(请检查您是否已用有效信息填写了所有字段,然后重试。谢谢)请将此行if(!isset($hasError))更改为if(!$hasError),并检查您是否还必须上载文件我收到相同的错误
if(trim($_FILES['datafile']['name'])==''){
 $hasError = true;
}
else {
    $target='upload/';/*give the directory name wfere you want to save it*/
move_uploaded_file($_FILES['datafile']['tmp_name'],$target);
}








  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
    <?php
$yourEmailAddress = '123@123.com'; //Put your own email address here.
if(trim($_FILES['datafile']['name'])==''){
     $hasError = true;
    }
    else {
        $target='upload/';/*give the directory name wfere you want to save it*/
    move_uploaded_file($_FILES['datafile']['tmp_name'],$target);
}
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
    $hasError = true;
} else {
    $subject = trim($_POST['subject']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!$hasError) {
    $emailTo = $yourEmailAddress;
    $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $comments";
    $headers = 'From: Interion Template <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
    mail($emailTo, $subject, $body, $headers);
    echo'<div id="success" class="sent success"><p><strong>Email Successfully Sent!</strong><br>Thanks for contacting Us. Your email was successfully sent and we \'ll be in touch with you soon.</p></div>';
} else { //If errors are found
    echo '<p class="error">Please check if you\'ve filled all the fields with valid information and try again. Thank you.</p>';
} 

 ?>
</head>

<body>
<form method="post" action="" enctype="multipart/form-data" id="contactform">
  <div class="response">&nbsp;</div>
  <p >
    <label for="contactname">What is your full legal Name?<span>*</span></label>
    <input id="contactname" type="text" value="" name="contactname" placeholder="Full Name" class="textflied">
    <i class="icon fa fa-user"></i></p>
  <p>
    <label for="email" >What is the best email address for us to contact you on?<span>*</span></label>
    <input id="email" type="text" value="" name="email" placeholder="Email Address" class="textflied">
    <i class="icon fa fa-envelope"></i></p>
  <p>
    <label for="subject" >Now tell us the best phone number to reach you at:<span>*</span></label>
    <input id="subject"  type="text" value="" name="subject" placeholder="Your phone number including area code" class="textflied">
    <i class="icon fa fa-phone  "></i></p>
  <p>
    <label for="message" >In a short paragraph how would you discribe your self?<span>*</span></label>
    <textarea id="message" type="text" name="message" value="" placeholder="Your short paragraph" rows="8" class="texttextarea"></textarea>
    <i class="icon fa fa-comments"></i></p>
  <label for="datafile" >Finally lets match the name with a face, upload your most recent photo<span>*</span></label>
  <input type="file" name="datafile" size="40">
  <br>
  <br>
  <p>
    <button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-disabled">SEND message</button>
  </p>
</form>
</body>
</html>