Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

php - How to send mutiply file in a HTML form?

I am designing a form for an e-commerce website using Prestashop 1.6, in this form I would like to allow customers to send multiple pictures.

For example : a customer wants to send back his order because he received the wrong product, but the customer service will need pictures of the packaging and proof that there is an issue, so he will need to upload his picture, then when the customer click on submit, I would like to send it in a email as attachment.

How can I proceed ?

<div class="box-container-1">
 <img src="http://dev.piscine-clic.com/img/icone-etat.svg" alt="photos">
  <label for="file">Envoyez nous des photos du matériel ainsi que de son emballage que
  vous souhaitez retourner</label>
<!-- INPUT pour envoyer les photos et preuves de retour -->
  <input class="file-button" type="file" id="file" name="file" multiple />
</div>

Thanks all for your future responses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your file input looks correct. On the server side, I would recommend using a class like PHPMailer, and then processing the form submit like so:

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $mail = new PHPMailer(true);
         
    try {
        $mail->isSMTP();
        $mail->Host = 'host';
        $mail->Username = 'username';
        $mail->Password = 'password';
        
        $mail->setFrom($from);
        $mail->addAddress($to);
        $mail->Subject = $subject;
        $mail->Body = $body;
        
        $file_count = count($_FILES['file']['name']);
        
        if ($file_count) {

            for($i=0 ; $i < $file_count ; $i++) {
                if (file_exists($_FILES['file']['tmp_name'][$i]) && is_uploaded_file($_FILES['file']['tmp_name'][$i])) {
                    
                    $file_path = $_FILES['file']['tmp_name'][$i];
                    $file_name = pathinfo($_FILES['file']['name'][$i], PATHINFO_FILENAME);
                    $file_ext = strtolower(pathinfo($_FILES['file']['name'][$i], PATHINFO_EXTENSION));
                    
                    $mail->addAttachment($file_path, $file_name . '.' . $file_ext);
                }
            }
        }

        $mail->send();

    } catch (Exception $e) {
        echo $mail->ErrorInfo;
        exit;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...