2016-12-20 123 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