dongyun8075 2016-11-09 17:09 采纳率: 100%
浏览 40

制作多重附件表格

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
Reply-To: webmaster@example.com";
        $headers .= "
MIME-Version: 1.0
Content-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!

  • 写回答

1条回答 默认 最新

  • donglu1971 2016-11-09 18:38
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?