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("antonio.junior98@libero.it");
$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. :)

2 Answers2

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
Juan Bonnett
  • 1,823
  • 1
  • 15
  • 33
  • 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. – Antonio Junior Aug 23 '15 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. – Juan Bonnett Aug 24 '15 at 02: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;
    }
}
Mani Singh
  • 107
  • 1
  • 2
  • 14