Help with code - multiple attachments

Hello all,

Before all, I want to excuse myself about the mimes mess. I’m really on the dark there. :sick:

Issue:
I’m getting one file attached, instead of two.

Request:
Can someone please lead me a hand here please?

The input fields have the same name following by ;

Here is what I think is the relevant code:

  //respeitante ao upload
   $allowedFileTypes = array('.JPEG','.jpeg', '.JPG','.jpg');

    $maxFileSize = 1048576; //20971520 (20MB) / 2097152 (2MB) / 5242880 (5MB) / 1048576 (1MB)

   $filesName = $_FILES['fotoUpload']['name']; //nome do ficheiro com extensão

    $filesTempName = $_FILES['fotoUpload']['tmp_name']; //nome temporário do ficheiro

    $filesType = $_FILES['fotoUpload']['type'];

    //$ext = strrchr($fileName, "."); //extensão apenas

    $emailID=md5(time());
 /*Headers*/
            $headers = 'From: Site Submission <'.$from.'>' . "\\r\
";
            $headers .= "MIME-Version: 1.0\\r\
";
            $headers .= "Content-Type: multipart/mixed;";
            $headers .= "boundary=$emailID\\r\
--".$emailID."\\r\
";

            $headers .= "Content-Type: text/html; charset=\\"UTF-8\\"\\r\
";
            $headers .= "Content-Transfer-Encoding: 7bit\\r\
\\r\
".$message."\\r\
--".$emailID."\\r\
";

            //anexa e aplica headers to attachment apenas se houver ficheiros. A ordem é importante.

            if(!empty($filesName))
            {
                for($x=0;$x<count($filesName);$x++)
                {
                    
                    //rb stands for read binary
                    $tmpFile = fopen($filesTempName[$x],"rb");

                    $data = fread($tmpFile,filesize($filesTempName[$x]));

                    fclose($tmpFile);

                    $data = chunk_split(base64_encode($data));

                    $headers .= "Content-Type: {\\"application/octet-stream\\"};\
" . " name=\\"$filesName[$x]\\"\
" .
                                "Content-Disposition: attachment;\
" . " filename=\\"$filesName[$x]\\"\
" .
                                "Content-Transfer-Encoding: base64\
\
" . $data . "\
\
";
                    $headers .= "--{$mime_boundary}\
";
        
                }
		
            }

Any help plz?

Regards,
Márcio

Just a guess it should be something like this:


$filesName[] = $_FILES['fotoUpload']['name'];

since the variable $filesName is supposed to be array.

But i would recommend to use PHPMailer for easy.

Hi, sorry for the late reply I’ve lost my internet connection. :s

Well, I have change to and I get an array that contains another array.
So, having $filesName is enough to have an array I presume, since what it gets filled with $_FILE.

Am I getting some misinterpretations or the cause should be somewhere else?

Maybe it’s not on the logic until here but in the MIMES ?

Please advice,
Márcio

Can you please post your HTML form?

This file is called candidatura_1.php

<form name="candidaturaOnline" action="candidatura_1.php" method="post" enctype="multipart/form-data">

                <fieldset id="fotografias">
                    <legend>Fotografias</legend>

                    <p>
                    <label id="labelFotoUpload" for="fotoUpload">Enviar fotografia 1 <em>(jpg, Max: 1 Mb)</em></label>
                    <input type="file" id="fotoUpload" name="fotoUpload[]"/><br />
                                        </p>

                    
                    <p>
                    <label id="labelFotoUpload2" for="fotoUpload2">Enviar fotografia 2 <em>(jpg, Max: 1 Mb)</em></label>
                    <input type="file" id="fotoUpload2" name="fotoUpload[]"/><br />
                                        </p>

                </fieldset>
                <input type="submit" value="Enviar Candidatura" name="submit"/>

</form>

Thanks a lot,
Márcio

ps- I will take all php code and post a paste bin later on.

And here is the php code:

http://pastebin.com/rFyh7vq0

Thanks for your time, in the meanwhile I will try the phpmailer you point out, but this one is so close that I’m wondering if it’s worth it.

Regards,
Márcio


$files = array();
for($i = 0; $i < count($_FILES['fotoUpload']['name']); $i++){
    $files[$i]['name'] = $_FILES['fotoUpload']['name'][$i];
    $files[$i]['tmp_name'] = $_FILES['fotoUpload']['tmp_name'][$i];
}
echo '<pre>'; print_r($files);

This is how the array files are handled. I hope you can loop through the $files array now.

And that did the trick, and allow me to have phpmailer with multiple attachments. :wink:

Thanks a lot,
Márcio