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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据