Multiple File upload and send as attachment

I searched the forums, but couldnt find a similar questions.
I want to create a form which has usual name, phone, location input fields and ability to upload 3-4 photos and send it by email.
Kindly let me know how to do this or point me to some resources.
Sorry, I am a PHP novice… Thanks to all in advance…

I’d highly recommend implementing SwiftMailer or phpMailer, both of which have easy ways of adding attachments to an email and sending the email vian SMTP or via PHP’s mail functionality.

Thank you @cpradio.
I could upload and send a single file with phpmailer.
But when I did few changes and tried multiple uploads and sending it failed.
There is something I am missing here, kindly take a look … thanks.

 * PHPMailer simple file upload and send example
  $msg = '';
  if (array_key_exists('userfile', $_FILES)) {

        foreach ($_FILES["file"]["error"] as $key => $error){
            if ($error == UPLOAD_ERR_OK) {
                $tmp_name = $_FILES["file"]["tmp_name"][$key];
                $name = $_FILES["file"]["name"][$key];
                move_uploaded_file($tmp_name," server/data/$name");
        if(move_uploaded_file($tmp_name,"server/data/$name" )){
            require 'lib/PHPMailerAutoload.php';
            $mail = new PHPMailer;
            $mail->setFrom('', 'From Doe');
            $mail->addAddress('', 'To Doe');
            $mail->Subject = 'PHPMailer file sender';
            $mail->msgHTML("My message body");
            if (!$mail->send()) {
                $msg = "Mailer Error: " . $mail->ErrorInfo;
            } else {
                $msg = "Message sent!";
                foreach ($_FILES["file"]["error"] as $key => $error)
                    $name = $_FILES["file"]["name"][$key];
        } else {
        $msg = 'Failed to move file to ' . $uploadfile;

<!DOCTYPE html>
    <meta charset="utf-8"/>
    <title>PHPMailer Upload</title>
<?php if (empty($msg)) { ?>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: 
        <input name="file" type="file" size="50" multiple>
        <input name="file[]" type="file" size="50" multiple>
        <input type="submit" value="Send File">
<?php } else {
    echo $msg;
} ?>

(Made these changes from hints on another post.)
Please help. Thanks a lot in advance.

Might be me, but it seems like you set $name within the foreach() loop, but then send the mail outside of that loop so it only runs for whatever the last value of $name is/was. You also seem to call move_uploaded_file() as part of the foreach() loop to move the file, but then try to move it again after the loop has done.

Do you want to send each uploaded file as an individual email, or send them all on a single mail? If the former is acceptable, surely moving the send-email code to the foreach() loop would do the trick. To send them all on one email, build an array of names in the foreach() loop, then add each of them to the email.

@droopsnoot, Thank you. I will try it out.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.