0

I'm using a form that allows the user to upload a single file and I am trying to make it be able to upload 4 files. I have tried loads of examples and had no luck in getting anything to work other than this single attachment upload where the file gets sent to my email:

HTML:

<form enctype="multipart/form-data" action="send_attachments_email.php" method="post">
<label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea>
<label for="file">File</label> <input type="file" name="file" id="file">
<input type="submit" name="submit" id="submit" value="send">
</form>

PHP:

<?php
    if(isset($_POST['submit']))
    {
        //The form has been submitted, prep a nice thank you message
        $output = '<h1>Thanks for your file and message!</h1>';
        //Set the form flag to no display (cheap way!)
        $flags = 'style="display:none;"';

        //Deal with the email
        $to = 'info@xxx.co.uk';
        $subject = 'a file for you';

        $message = strip_tags($_POST['message']);
        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
        $filename = $_FILES['file']['name'];

        $boundary =md5(date('r', time())); 

        $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

        $message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

        mail($to, $subject, $message, $headers);
    }
?>

Would I need to make file an array and would the php still work? Or what is the best way to go about it?

Thanks!

Bethfiander
  • 167
  • 1
  • 2
  • 14
  • I have tried changing the php to have an array: `$filename = $_FILES['file[]']['name']['0']; $filename1 = $_FILES['file[]']['name']['1']; $filename2 = $_FILES['file[]']['name']['2']; $filename3 = $_FILES['file[]']['name']['3'];` but this wont send anything – Bethfiander Nov 09 '16 at 17:39
  • Are you able to select multiple files? – TheTom Nov 09 '16 at 17:55
  • Thanks for the response. Yes I am able to select multiple files on the web page, but when I press send I get a blank email. If I use the code pasted I get an email with one photo but trying to add an array I just get blanks. – Bethfiander Nov 09 '16 at 18:07
  • 1
    Have you tried [0] on chunking either? And what you've tried ['0'] is wrong way try right one and then let me know. – TheTom Nov 09 '16 at 18:19
  • 1
    $_FILES['file']['tmp_name'][0] like this or in loop. – TheTom Nov 09 '16 at 18:21

1 Answers1

0

The name of the input should indicate that there will be multiple values/that an array is expected. Try changing <input type="file" name="file" id="file"> to <input type="file" name="file[]" id="file">.

http://php.net/manual/en/features.file-upload.multiple.php

kyle
  • 2,563
  • 6
  • 22
  • 37
  • Hi Kyle thanks alot for the response and link, the link seems to be uploading to a server not email. I have tried doing this: `` and in my php changed to this: `$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'][0]))); $filename = $_FILES['file[]']['name'][0];` Email is coming through but giving me a made up attachment not the image in uploading so I assume it isnt saving the file in the array - Am i missing something in the php or is it completely wrong? Thanks – Bethfiander Nov 10 '16 at 10:30
  • Ah I see, so this is a very different problem. Do you have the ability to use a library such as [PHP mailer](https://github.com/PHPMailer/PHPMailer)? Sending attachments via PHPs mail function can be a little tricky... If you can't use a library look at http://stackoverflow.com/a/27565430 – kyle Nov 10 '16 at 22:27