使用PHP

时间:2017-01-26 17:21:37

标签: php html wordpress file

我刚刚为我的一个wordpress网站完成了一个HTML表单,效果很好。提交时,它会在WP_User数据库表中将该人员创建为用户,并向我发送一封电子邮件,其中包含表单中的所有信息。但是,我的html表单允许用户上传所需的文件,但电子邮件只显示文件名。我正在寻找一种方法来通过电子邮件发送要查看和下载的实际文件。到目前为止,这是我的代码:

    <div id="container">
    <form method="post" name="myForm">
    <h1 align="center">Welcome!</h1>
    <h2 align="center">Thank you for your interest. We look forward to working with you.</h2>
    <h3>First Name</h3> <input type="text" name="fName" style="width: 355px;"/>
    <h3>Last Name</h3> <input type="text" name="lName" style="width: 355px;"/>
   <h3>Company Name</h3> <input type="text" name="compName" style="width: 355px;"/>
   <h3>Business Needs</h3> 
   <select name="businessNeeds" style="width: 355px;">
    <option value="">Select...</option>
    <option value="IntDesign">Interior Designer</option>
    <option value="Ecom">E-Commerce Only</option>
    <option value="Retail">Retail Store Only</option>
    <option value="RetailEcom">Retail and E-Commerce</option>
    <option value="Mult">Multiple Locations</option>
  </select>
   <h3>Address</h3> <input type="text"  name="address" style="width: 355px;"/>
   <h3>City</h3> <input type="text"  name="city"  style="width: 355px;"/>
   <h3>State</h3> <input type="text"  name="state"  style="width: 355px;"/>
   <h3>Zip</h3> <input type="text"  name="zip" style="width: 355px;"/>
   <h3>Phone</h3> <input type="text"  name="phone" style="width: 355px;"/>
    <h3>Email</h3>  <input id="email" type="text" name="uemail" style="width: 355px;"/>
    <h3>Create a Username</h3> <input type="text"  name="uname" style="width: 355px;"/>
    <h3>Create a Password</h3> <input type="password"  name="pass" style="width: 355px;"/>
    <h3>Confirm Password</h3> <input type="password"  name="ConfPass" style="width: 355px;"/>
    <h3>Sales Tax ID</h3> <input type="text" name="taxID" style="width: 355px;"/>
    <h3>Upload Tax ID Certificate</h3> <input type="file" name="fileUpload" style="width: 355px;"/>
    <input type="submit" value="Submit" />
</form>
</div>


<?php
function create_account(){

$fName = ( isset($_POST['fName']) ? $_POST['fName'] : '' );
$lName = ( isset($_POST['lName']) ? $_POST['lName'] : '' );
$compName = ( isset($_POST['compName']) ? $_POST['compName'] : '' );
$businessNeeds = ( isset($_POST['businessNeeds']) ? $_POST['businessNeeds'] : '' );
$address = ( isset($_POST['address']) ? $_POST['address'] : '' );
$city = ( isset($_POST['city']) ? $_POST['city'] : '' );
$state = ( isset($_POST['state']) ? $_POST['state'] : '' );
$zip = ( isset($_POST['zip']) ? $_POST['zip'] : '' );
$phone = ( isset($_POST['phone']) ? $_POST['phone'] : '' );
$email = ( isset($_POST['uemail']) ? $_POST['uemail'] : '' );
$user = ( isset($_POST['uname']) ? $_POST['uname'] : '' );
$pass = ( isset($_POST['upass']) ? $_POST['upass'] : '' );
$ConfPass = ( isset($_POST['ConfPass']) ? $_POST['ConfPass'] : '' );
$taxID = ( isset($_POST['taxID']) ? $_POST['taxID'] : '' );
$fileUpload = ( isset($_POST['fileUpload']) ? $_POST['fileUpload'] : '' );

$email_from = 'admin';
$email_subject = "New Wholesale Form Submission";
$message = "You have received a new message from: \n";
$message .= "First Name: " .$_POST["fName"]. "\n";                            
$message .= "Last Name: ".$_POST["lName"]. "\n";
$message .= "Company Name: ".$_POST["compName"]. "\n";
$message .= "Business Type: " .$_POST["businessNeeds"]. "\n";
$message .= "Address: ".$_POST["address"]. "\n";
$message .= "City: ".$_POST["city"]. "\n";
$message .= "State: ".$_POST["state"]. "\n";
$message .= "Zip: ".$_POST["zip"]. "\n";
$message .= "Phone: ".$_POST["phone"]. "\n";
$message .= "Email: ".$_POST["uemail"]. "\n";
$message .= "Tax ID: ".$_POST["taxID"]. "\n";
$message .= "Certificate: ".$_POST["fileUpload"]. "\n";


$to = "email";
$headers = "From: $email_from \r\n";



if ( !username_exists( $user )  && !email_exists( $email ) ) {
   $user_id = wp_create_user( $user, $pass, $email );
   if( !is_wp_error($user_id) ) {
       //user has been created
       $user = new WP_User( $user_id );
       $user->set_role( 'subscriber' ); 


       wp_mail( $to, $email_subject, $message, $headers );


       //Redirect
       wp_redirect( 'URL' );
       exit;
   } else {
       //$user_id is a WP_Error object. Manage the error
   }
}

}
add_action('init','create_account');
?>

1 个答案:

答案 0 :(得分:1)

您需要在表单标记中添加enctype="multipart/form-data"

enctype属性设置或返回表单中enctype属性的值。

enctype属性指定在将表单数据发送到服务器之前应如何对其进行编码。

表单数据被编码为&#34; application / x-www-form-urlencoded&#34;默认情况下。这意味着所有字符在发送到服务器之前都会被编码(空格转换为&#34; +&#34;符号,特殊字符转换为ASCII HEX值)。

in multipart / form-data不编码任何字符。使用具有文件上载控件的表单时需要此值