M
M
MeMoJlor2020-09-22 00:54:22
PHP
MeMoJlor, 2020-09-22 00:54:22

How to attach multiple files to an email using PHPmailer?

There is a code that can send only one picture. I duplicate functions, still only one picture comes.

Working code:

<input type="file" name="myfile[]" multiple id="myfile">


$file = $_FILES['myfile'];

if (!empty($file['name'][0])) {
    for ($ct = 0; $ct < count($file['tmp_name']); $ct++) {
        $uploadfile = tempnam(sys_get_temp_dir(), sha1($file['name'][$ct]));
        $filename = $file['name'][$ct];
        if (move_uploaded_file($file['tmp_name'][$ct], $uploadfile)) {
            $mail->addAttachment($uploadfile, $filename);
            $rfile[] = "Файл $filename прикреплён";
        } else {
            $rfile[] = "Не удалось прикрепить файл $filename";
        }
    }   
}


Duplicate like this:

$file = $_FILES['myfile'];

if (!empty($file2['name'][0])) {
    for ($ct = 0; $ct < count($file['tmp_name']); $ct++) {
        $uploadfile = tempnam(sys_get_temp_dir(), sha1($file2['name'][$ct]));
        $filename = $file2['name'][$ct];
        if (move_uploaded_file($file2['tmp_name'][$ct], $uploadfile)) {
            $mail->addAttachment($uploadfile, $filename);
            $rfile[] = "Файл $filename прикреплён";
        } else {
            $rfile[] = "Не удалось прикрепить файл $filename";
        }
    }   
}


I myself do not believe in the work of a duplicate, because there are still many variables.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-09-22
@MeMoJlor

Good morning.
You get the first element of the array and skip the rest. Wrap it in a loop
if (!empty($file['name'][0])) {
if (!empty($file['name'][$k])) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question