2015-10-04 54 views
0

我知道它已經問過的問題而已,我剛纔提到那些沒有解決方案已經從他們如此我張貼我自己的問題,對不起你們需要幫助限制php文件上傳文件類型的PDF和DOC,DOCX

發現問題我有一個PHP附件從它發送附件到我的電子郵件,因爲我已經限制文件類型只接受PDF和文檔,但它不能正常工作,並且我也限制了文件大小,也不工作,我會發布我的PHP和HTML代碼在這裏請糾正我,如果我錯了,請提前致謝

HTML表格

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Simple Ajax Contact Form</title> 
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
<script type="text/javascript" src="js/script.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
<script type="text/javascript" src="js/jquery.validate.js"></script> 
<script type="text/javascript" src="js/additional-methods.js"></script> 
</head> 
<body> 
<button type="button" class="btn btn-danger btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> 

<!-- Modal --> 
<div id="myModal" class="modal fade" role="dialog"> 
    <div class="modal-dialog"> 

    <!-- Modal content--> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <h4 class="modal-title">Modal Header</h4> 
     </div> 
     <div class="modal-body"> 
     <div class="form-style" id="contact_form"> 
    <div class="form-style-heading">Please Contact Us</div> 
    <div id="contact_results"></div> 
    <div class="form-group"> 
     <label><span>Name <span class="required">*</span></span> 
      <input type="text" name="name" id="name" required="true" class="form-control"/> 
     </label> 

    </div> 
     <div class="form-group"> 
       <label><span>Email <span class="required">*</span></span> 
      <input type="email" name="email" required="true" class="form-control"/> 
     </label> 
     </div> 
    <div class="form-group"> 
      <label><span>Phone <span class="required">*</span></span> 
      <input type="text" name="phone" maxlength="15" required="true" class="form-control" /> 
     </label> 

    </div> 
    <div class="form-group"> 
     <label><span>Attachment</span> 
      <input type="file" accept="doc,pdf,docx" required="true" name="file_attach" class="form-control" /> 
     </label> 
     </div> 
     <div class="modal-footer"> 

    <label> 
      <span>&nbsp;</span><input type="submit" id="submit_btn" class="btn btn-primary" value="Submit" /> 

     </label> 

     </div> 
     </div> 
</div> 
    </div> 

    </div> 
</div> 

</body> 
</html> 

PHP

<?php 
if($_POST) 
{ 
    $to_email  = "[email protected]"; //Recipient email, Replace with own email here 
    // $from_email = "[email protected]"; //From email address (eg: [email protected]) 

    //check if its an ajax request, exit if not 
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 
     $output = json_encode(array(//create JSON data 
      'type'=>'error', 
      'text' => 'Sorry Request must be Ajax POST' 
     )); 
     die($output); //exit script outputting json data 
    } 

    //Sanitize input data using PHP filter_var(). 
    $user_name  = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING); 
    $user_email  = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL); 
    $phone_number = filter_var($_POST["phone_number"], FILTER_SANITIZE_NUMBER_INT); 
     if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error. 
     $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!')); 
     die($output); 
    } 
    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation 
     $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!')); 
     die($output); 
    } 
    if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field 
     $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number')); 
     die($output); 
    } 

    //email body 
    $message_body = $message."\n\n".$user_name."\nEmail : ".$user_email."\nPhone Number :". $phone_number ; 

    ### Attachment Preparation ### 
    $file_attached = false; 
    if(isset($_FILES['file_attach'])) //check uploaded file 
    { 
     //get file details we need 
     $file_tmp_name = $_FILES['file_attach']['tmp_name']; 
     $file_name  = $_FILES['file_attach']['name']; 
     $file_size  = $_FILES['file_attach']['size']; 
     $file_type  = $_FILES['file_attach']['type']; 
     $file_error  = $_FILES['file_attach']['error']; 

     //exit script and output error if we encounter any 
     if($file_error>0) 
     { 
      $mymsg = array( 
      1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
      2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
      3=>"The uploaded file was only partially uploaded", 
      4=>"No file was uploaded", 
      6=>"Missing a temporary folder"); 

      $output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error])); 
      die($output); 
     } 

     //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)); 
     //now we know we have the file for attachment, set $file_attached to true 

    $allowedExts = array("pdf", "doc", "docx"); 
    $extension = end(explode(".", $_FILES["file"]["name"])); 
    if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) 
    { 
     if ($_FILES["file"]["error"] > 0) 
     { 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
     } 
     else 
     { 

$file_attached = true; 
     } 
    } 

    } 

    if($file_attached) //continue if we have the file 
    { 
     $boundary = md5("sanwebe"); 

     //header 
     $headers = "MIME-Version: 1.0\r\n"; 
     $headers .= "From:".$user_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/plain; charset=ISO-8859-1\r\n"; 
     $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
     $body .= chunk_split(base64_encode($message_body)); 

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

    }else{ 
     //proceed with PHP email. 
     $headers = "From:".$user_email."\r\n". 
     'Reply-To: '.$user_email.'' . "\n" . 
     'X-Mailer: PHP/' . phpversion(); 
     $body = $message_body; 
    } 

    $send_mail = mail($to_email, $subject, $body, $headers); 


    if(!$send_mail) 
    { 
     //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens) 
     $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.')); 
     die($output); 
    }else{ 
     $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email')); 
     die($output); 
    } 

} 

?> 

回答

0
if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) 
{ 

我想,你的意思是:

if ((($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword")) && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) 
{ 
+0

它不工作 – devbj