php和html表单,文件上传无效

时间:2017-11-04 09:19:44

标签: php html

<form action="mail/career-mail.php" id="work-form" method="post">
     <div class="form-group text-center heading">
        <h2>Work With Us</h2>
     </div>

     <div class="form-group"> 
        <input type="text" class="form-control" name="name" placeholder="Name" required="required">
     </div>

     <div class="form-group"> 
        <input type="text" class="form-control" name="contact" placeholder="Contact Number" required="">
     </div>

     <div class="form-group"> 
        <input type="email" class="form-control" name="email" placeholder="Email" required="">
     </div>

     <div class="form-group"> 
         <select class="form-control" id="designation" name="designation"> 
         <option value="none">Select Designation</option> 
            <option value="BDM">BDM</option> 
            <option value="BDE">BDE</option> 
            <option value="Operations Manager">Operations Manager</option> 
            <option value="Research Analyst (Voice and/or Data)">Research Analyst (Voice and/or Data)</option> 
            <option value="Team Lead">Team Lead</option> 
            <option value="Quality Analyst">Quality Analyst</option> 
            <option value="MIS">MIS</option> 
            <option value="Human Resource (Generalist)">Human Resource (Generalist)</option> 
            <option value="others">Others</option>
         </select>
     </div>

     <div class="form-group otherblock"> 
        <textarea name="others" name="others" placeholder="Other"></textarea>
     </div> 

     <div class="form-group file-upload"> 
        <label>Upload Resume</label>  
             <input type="file" id="field40707" name="field40707">  
        <p>limit to 1 mb. File types, .doc, .docx, .pdf</p>                 
     </div>

     <div class="form-group">  
        <select class="form-control"> 
           <option value="none">How did you hear about us?</option> 
           <option value="Intranet">Intranet</option> 
           <option value="LinkedIn">LinkedIn</option> 
           <option value="via Job website">Via Job Website</option>   
        </select>                         
     </div>

     <div class="form-group radio-wrap"> 
        <label>Do you authorize a Representative from HQL Solutions to contact you.</label>               
        <div class="inline-block"> 
           <input type="radio" name="referance" value="yes" checked=""> Yes<br>
        </div> 
        <div class="inline-block"> 
           <input type="radio" name="referance" value="no"> No<br>
        </div>  
     </div> 

     <div class="form-group text-center mr-b-0">
         <button type="submit" class="btn btn-orange">Submit</button>
     </div>
  </form> <!-- work-form --> 

<?php
if($_POST){
    $name = $_POST['name'];
    $contact = $_POST['contact'];
    $email = $_POST['email'];
    $designation = $_POST['designation'];
    $others = $_POST['others'];
    $field40707 = $_POST['field40707'];
    $referance = $_POST['referance'];   

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: noreply@hqlsolutions.com' . "\r\n";

    $to  = "niranjan.pawar2@gmail.com" ;

    $subject = "Careers form ";  

    $message .= " Name :". $name."\r\n";
    $message .= " Contact :". $contact."\r\n";
    $message .= " Email :". $email."\r\n";
    $message .= " Designation :". $designation."\r\n";
    $message .= " Others :". $others."\r\n"; 
    $message .= " field40707 :". $field40707."\r\n";
    $message .= " Referance :". $referance."\r\n";

    var_dump($name);
    var_dump($contact);
    var_dump($email);
    var_dump($designation);
    var_dump($others);
    var_dump($field40707);
    var_dump($referance);

    //get file details we need
    $file_tmp_name    = $_FILES['resume']['tmp_name'];
    $file_name        = $_FILES['resume']['name'];
    $file_size        = $_FILES['resume']['size'];
    $file_type        = $_FILES['resume']['type'];
    $file_error       = $_FILES['resume']['error'];

    $user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);

    if($file_error>0)
    {
        die('upload error');
    }
    //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: ".$user_email."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/html; 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;

    // if(mail($to,$subject,$body,$headers)){
    // echo "mail sent";  
    // else{
    // echo "Try agin later";   
    // };

    if(mail($to,$subject,$message,$headers)){
        echo "mail sent";   
    }
    else{
        echo "not sent";
        phpinfo();
    };
}
?>

有两个代码块,一个用于html,第二个用于php邮件功能。当我点击提交预期结果需要获取邮件中的所有表单值,但输出带有空邮件。

请你帮忙,因为当我转储变量时它会显示值但是在邮件中它是空的。提前谢谢。

1 个答案:

答案 0 :(得分:0)

当您处理包含

的表单中的文件时
<form action="mail/career-mail.php" enctype='multipart/form-data' id="work-form" method="post">

</form>