Limitation type et taille document

Résolu/Fermé
debtech Messages postés 12 Date d'inscription mardi 10 octobre 2017 Statut Membre Dernière intervention 28 mars 2020 - 29 nov. 2019 à 11:02
yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 - 3 déc. 2019 à 17:56
Bonjour quelqu'un pourrait il m'aider, un envoi de mail avec phpmailer qui fonctionne super bien mais j'aimerais mettre des conditions sur le type de document a télécharger (pdf,jpg) et aussi la taille qui n’excède pas 4MB. Je n'arrive a pas a le faire, merci de m'aider svp

 <form class="needs-validation" action="send_mail.php" method="post">
<div>
    <input type="file" name="attachmentFile" id="attachmentFile" class="form-control"  id="Input" placeholder="Joindre fichier" required>
<button type="submit" class="btn btn btn-danger btnAction">Soumettre</button>
</form>


$attachmentFile = $_FILES['attachmentFile']['tmp_name'];
$mail = new PHPMailer(true);
try {
    $mail->SMTPDebug = 0;  
    $mail->isSMTP(); 
    $mail->Host       = 'XXX'; 
    $mail->SMTPAuth   = true;  
    $mail->AuthType = "PLAIN";
    $mail->Username   = 'XXX';   
    $mail->Password   = 'XXX';  
    $mail->SMTPSecure = 'tls';  
    $mail->Port       = '587'; 
    $mail->setFrom("$email");
    $mail->addAddress('xxx@gmail.com');
    $mail->isHTML(true);    
    $mail->Subject = "document";
    $mail->Body    = $attachmentFile ;
    $mail->send();
    echo 'document envoyé avec succès. Merci!';
}   
catch (Exception $e) {
    echo "document non envoyé, veuillez réessayer.";
}

1 réponse

yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 1 477
29 nov. 2019 à 14:48
bonjour, qu'as-tu essayé?
0
debtech Messages postés 12 Date d'inscription mardi 10 octobre 2017 Statut Membre Dernière intervention 28 mars 2020
3 déc. 2019 à 15:40
en cherchant un peu j'ai pu faire ceci, et ca marche. merci

<?php
    require('../phpmailerCommande/class.phpmailer.php');

    $attachmentFile = $_FILES['attachmentFile']['tmp_name'];

    if(isset($_FILES['attachmentFile'])) {
        $errors     = array();
        $maxsize    = 2097152;
        $acceptable = array(
            'application/pdf',
            'application/msword',
            'image/jpeg',
            'image/jpg',
            'image/gif',
            'image/png'
        );    
        if(($_FILES['attachmentFile']['size'] >= $maxsize) || ($_FILES["attachmentFile"]["size"] == 0)) {
            $errors[] = 'File too large. File must be less than 2 megabytes.';
        }   
        if((!in_array($_FILES['attachmentFile']['type'], $acceptable)) && (!empty($_FILES["attachmentFile"]["type"]))) {
            $errors[] = 'Invalide type de fichier. Type de fichier téléchargeable : PDF, JPG, GIF, DOC.';
        }
    
        if(count($errors) === 0) {
                                    $mail = new PHPMailer(true);

                                    try {
                                        $mail->SMTPDebug = 0;  
                                        $mail->isSMTP(); 
                                        $mail->Host       = 'xxxx'; 
                                        $mail->SMTPAuth   = true;  
                                        $mail->AuthType = "PLAIN";
                                        $mail->Username   = 'xxx';   
                                        $mail->Password   = 'xxx';  
                                        $mail->SMTPSecure = 'tls';  
                                        $mail->Port       = '587'; 

                                        $mail->setFrom("$email");
                                        $mail->addAddress('xxx@gmail.com');

                                        $mail->isHTML(true); 
                                        $mail->Subject = "$objet";
                                        $mail->Body    = $attachmentFile;
                                        
                                        $mail->AddAttachment($attachmentFile, 'demo'); 

                                        $mail->send();
                                        echo 'Fichier envoyé, Merci!';
                                    }   
                                    catch (Exception $e) {
                                        echo "Fichier non envoyé, veuillez réessayer.";
                                    }
        } else {
            foreach($errors as $error) {
                echo '<script>alert("'.$error.'");</script>';
            }
    
            die(); //Ensure no more processing is done
        }
    }


?>
0
yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 1 477 > debtech Messages postés 12 Date d'inscription mardi 10 octobre 2017 Statut Membre Dernière intervention 28 mars 2020
3 déc. 2019 à 17:56
parfait! peux-tu alors marquer le sujet comme résolu?
0