1

I have a simple form with:

    <form>
    ....
    <input type="hidden" name="MAX_FILE_SIZE" value="100000"> <input name="allegato" type="file" multiple>
    ....
    </form>

And the validete script php builded with PHPmailer:

<?php session_start();

//Percorso dove raccoglieremo gli allegati
$percorso = "allegati";

//Raccolta dati form 
$marca = $_POST['marca'];
$modello = $_POST['modello'];
$immatricolazione = $_POST['immatricolazione'];
$carburante = $_POST['carburante'];
$cilindrata = $_POST['cilindrata'];
$nditarga = $_POST['nditarga'];

$infosinistro = $_POST['infosinistro'];

$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];

$datipers = $_POST['datipers'];
$newsletter = $_POST['newsletter']; 

//Upload dell'allegato
move_uploaded_file($_FILES['allegato']['tmp_name'], $percorso.$_FILES['allegato']['name']);

//Messaggio
$oggetto = "Nuova richiesta preventivo";
$messaggio = " Hai ricevuto i seguenti dati da $nome $cognome <br> Marca: $marca <br> Modello: $modello \r\n Immatricolazione: $immatricolazione \r\n Carburante: $carburante \r\n Cilindrata: $cilindrata \r\n N. di Targa: $nditarga \r\n Maggiori info sul Sinistro: $infosinistro \r\n\r\n Dati del cliente: $nome $cognome \r\n N. di telefono: $telefono \r\n Indirizzo e-mail: $email \r\n\r\n $datipers \r\n $newsletter \r\n";
//Inclusione della libreria
require_once("class.phpmailer.php");
//Creiamo un oggetto PHPMailer e ne settiamo le variabili
$mail = new PHPMailer();
$mail->From = "$email";
$mail->FromName = "$email";
$mail->AddAddress("[email protected]");
$mail->IsHTML(true);
$mail->Subject = $oggetto;
$mail->Body = $messaggio;
$mail->AddAttachment($percorso.$_FILES['allegato']['name']);
//Inviamo l'email
if($mail->Send())
echo "Email inoltrata correttamente";

?>

My problem are multiple attachments. I do not know how to add more. It is a month that I work, but I can't. Do you have any solutions please? Thank you.

Antonio Jr. :)

5
  • what happens when if add another $mail->AddAttachment();?
    – HPierce
    Aug 22, 2015 at 20:01
  • 1
    Include your attachment code in for loop. Refer this link stackoverflow.com/questions/22245082/…
    – Talk2Nit
    Aug 22, 2015 at 20:05
  • Since multiple upload button is in question - you probably got array as result, so loop through it, and attach files...
    – sinisake
    Aug 22, 2015 at 20:05
  • @HPierce I already tried and doesn't work ... Aug 23, 2015 at 15:00
  • @Nitin I don't understand ... already tried...and doesn't work! Aug 23, 2015 at 15:02

2 Answers 2

1

Change

$mail->AddAttachment($percorso.$_FILES['allegato']['name']);

To

$mail->AddAttachment($percorso.$_FILES['allegato']['tmp_name']);

And use the method again with another file, it will add it to the attached files!

EDIT:

I realised you have the "multiple" attribute in your file input.

You want all those files to be attached. OK, we have to know that when you have "multiple" attribute the files will be stored in a 'weird' array.

The first file would be $_FILES['inputname']['tmp_name'][0] The second file would be $_FILES['inputname']['tmp_name'][1]

And so on. That's the way you will be adding these attachments.

If you want to get the size of a multiple uploaded files you would use:

`$_FILES['inputname']['size'][0]` -> Size of the first file
`$_FILES['inputname']['size'][1]` -> Size of the second file
2
  • Hi, thank you for the reply. It doesn't work. I don't know if I'm wrong. Below you can see how I have changed. Aug 23, 2015 at 15:22
  • Remember that when you have the MULTIPLE attribute in your File Input, you will need to accesse each file with its position in the array. --- $mail->AddAttachment($percorso.$_FILES['allegato']['tmp_name'][0]); would attach the first file, and so on. Aug 24, 2015 at 2:07
0

Try this one

for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
    $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
    $filename = $_FILES['userfile']['name'][$ct];
    if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
        $mail->addAttachment($uploadfile, $filename);
    } else {
        $msg .= 'Failed to move file to ' . $uploadfile;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.