2016-12-20 129 views
0

我怎麼能改變我的代碼只接受一些擴展。允許的擴展名phpmailer

看我的代碼:

<?php 
    ob_start(); 
    $_SESSION['nomecomp'] = $_POST['nomecomp']; 

     $email_env = $_POST['email_env']; 
     if (isset($email_env)) { 
    //variaveis vindas da pagina 
    $varcritico = $_POST['varcritico']; 
    $nomecomp = $_POST['nomecomp']; 
    $chapa = $_POST['chapa']; 
    $funcao = $_POST['funcao']; 
     $setor = $_POST['setor']; 
     $unidade = $_POST['unidade']; 
     $deschelp = $_POST['deschelp']; 





    //variveis do modal 
    //$email_env = $_POST['email_env']; 
     //$senha_env = $_POST['senha_env']; 






     <td>$deschelp</td> 



     </tr> 
     </table>'"; 

     /** 
     * PHPMailer multiple files upload and send example 
      */ 

     $msg = ''; 
      //if (array_key_exists('userfile', $_FILES)) { 

     // Create a message 
     // This should be somewhere in your include_path 
      include ("lib/PHPMailerAutoload.php"); 
     $mail = new PHPMailer(); 

我曾嘗試添加一些代碼,但不做出sucess,例如,我可以嘗試推陣列,看看是否延長一些數組裏面?

謝謝。

+0

首先,建立允許擴展($ AllowedFileTypes)的陣列。其次,獲取上傳文件的擴展名($ FileExtension),然後檢查擴展名是否爲in_array($ FileExtension,$ AllowedFileType) – JustBaron

+0

,如下所示:'$ allowed = array('pdf','doc','docx' ,'gif','jpeg','jpg','png','rtf','txt','zip');' –

+0

是的,這樣的效果。然後,在提交之前驗證表單或驗證文件類型/擴展名,然後從tmp localtion – JustBaron

回答

0

假設你想移動它們之前檢查上傳的文件類型:

$AllowedFileTypes = array("pdf","txt"); // build array 
$FileName = $_FILES['userfile']['name']; // get filename of file input 
$FileType = end((explode(".", $FileName))); // get file type/extension 
if(in_array($FileType, $AllowedFileTypes)){ // check to see if file type is allowed 
// perform copy/move file 
} 
else{ 
// ignore/alert/whatever 
} 

注意:您可能需要修改的變量,以滿足您的要求。

如果你想在提交表單之前驗證文件類型/擴展,看看jQuery驗證文件輸入:https://stackoverflow.com/a/20929391/715105