当前位置: 首页 > 知识库问答 >
问题:

如何将文件上传到我现有的联系表单[重复]

井旺
2023-03-14

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

    <?php
$yourEmailAddress = '123@123.com'; //Put your own email address here.

//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(!isset($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>';
} 

 ?>

这是我的超文本标记语言部分

<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>

共有1个答案

邓嘉致
2023-03-14

请添加此代码并检查它可能对您有帮助

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>
 类似资料:
  • 依赖模块 安装依赖 npm install --save busboy busboy 是用来解析出请求中文件流 例子源码 demo源码 https://github.com/ChenShenhai/koa2-note/blob/master/demo/upload/ 封装上传文件到写入服务的方法 const inspect = require('util').inspect const path

  • 问题内容: 我有一个主类,期望使用-D选项传递某些属性。我可以通过将其作为VM选项发送来在IDE中访问它。 我使用Maven并在尝试以下操作时将此应用程序打包到jar文件中: 要么 没有获取环境系统属性。 关于发生了什么的任何指示? 问题答案: 在。之前传递参数。如果在jar文件之后传递它们,它们将被解释为命令行参数并传递给in 。喜欢,

  • 本文向大家介绍layui 实现表单和文件上传一起传到后台的例子,包括了layui 实现表单和文件上传一起传到后台的例子的使用技巧和注意事项,需要的朋友参考一下 HTML代码 JavaScript代码 以上这篇layui 实现表单和文件上传一起传到后台的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。

  • 问题内容: 我创建了一个从有权访问的FTP服务器下载文件的功能。如何将文件上传回FTP服务器? 以下是我使用的download_files方法: 问题答案: 使用Apache Commons Net库中的FTPClient类。 这是一个带有示例的代码段: 摘录自http://www.kodejava.org/examples/356.html

  • 问题内容: 我认为,当jenkins构建静态网站时,我有一个简单的用例,因此在构建结束时,我有一个类似于$ WORKSPACE / site-result的文件夹。 现在,我想将此文件夹上传到S3(如果已有存储,请清理存储桶)。我该怎么做? 我正在使用管道,但是如果需要可以切换到自由样式项目。到目前为止,我已经安装了S3插件(S3发布者插件)。已创建IAM用户。向“配置系统”部分添加了凭据。并且找

  • 我看的是: > 与谷歌云平台集成 当前代码: