制作多重附件表格

时间:2016-11-09 17:09:12

标签: php html forms post file-upload

我正在使用允许用户上传单个文件的表单,我正在尝试使其能够上传4个文件。我已经尝试了大量的示例,除了这个文件被发送到我的电子邮件的单个附件上传之外,没有任何运气可以运行:

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);
    }
?>

我需要将文件设为数组吗?php还能运行吗?或者最好的方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

输入的名称应该表明预期会有多个值/数组。尝试将<input type="file" name="file" id="file">更改为<input type="file" name="file[]" id="file">

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