【问题标题】:Allowing Specific File Types to be Uploaded in PHP Email Forms允许在 PHP 电子邮件表单中上传特定文件类型
【发布时间】:2017-08-02 08:11:05
【问题描述】:

我正在创建一个 php 表单。我想允许用户上传 mp3 文件、pdf、png 或 jpg。表格的所有其他方面似乎都有效;但是,附件的大小为 0 kb。在我的 php.ini 中,我有一个很大的文件大小限制。以下是我正在使用的代码:

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

    //Email
    $to = 'info@music-scribes.com';
    $subject=" $servicetype Music Scribes Inquiry";

    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $servicetype=$_REQUEST['servicetype'];
    $message = strip_tags($_POST['message']);
    $attachment = $_FILES['file']['tmp_name'];
    $file = $_FILES['file']['name'];

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

    $headers = "From: $name<$email>\r\nReturn-path: $email";
    $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

    Service Type: $servicetype
    Message: $message 

    --_2_$boundary--
    --_1_$boundary
    Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3, application/pdf, image/jpeg, image/png; name=\"$file\" 
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment 

    $attachment
    --_1_$boundary--";

    mail($to, $subject, $message, $headers);
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>MailFile</title>
    </head>

    <body>

    <?php echo $output; ?>

    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
    <font size=2>

    <p><label for="name">Name<font color=red>*</font></label><br><input type="name" name="name" required><br><br>
    <p><label for="email">Email<font color=red>*</font></label><br><input type="email" name="email" required><br><br>

    Transcription Service Type:
    <select name="servicetype">
    <option value="Transcription">Transcription</option>
    <option value="Arrangement">Arrangement</option>
    <option value="Scoring">Scoring</option>
    <option value="Other">Other</option>
    </select><br><br>

    <p><label for="message">Message<font color=red>*</font></label><br>
    <textarea name="message" id="message" cols="40" rows="8" required>
    </textarea></p><br>

    <p><label for="file">Upload File</label> <input type="file" name="file" id="file"></p>
    <p><input type="submit" name="submit" id="submit" value="send"></p>
    </form>
    </body>
    </html>

请让我知道我必须更改哪些内容才能使 mp3 上传、pdf 和图像都通过表单工作。谢谢!

【问题讨论】:

    标签: php forms file email upload


    【解决方案1】:

    所以文件已正确上传,但无法将它们添加到您的电子邮件中?

    我建议使用第三方库来简化操作。如果您想发送简单的电子邮件消息,PHP 的邮件功能可以正常工作,但如果您想发送附件或在电子邮件中使用 HTML,事情会变得稍微复杂一些。当然有可能,但像 PHPMailer 这样的库让事情变得更容易。

    在它的 Github 页面上,有一个完整的示例说明如何使用它。要添加附件,您可以使用$email-&gt;AddAttachment('path/to/file.type', 'fileName.type');

    【讨论】:

      【解决方案2】:

      您在哪里上传文件?我没有看到任何文件上传处理。 看看这个例子:Handling file uploads(尤其是在 move_uploaded_file 部分)。

      为什么要在代码中混合 REQUEST 和 POST 超全局变量?你有$_REQUEST['email']$_POST['message']。如果所有数据都来自post请求,请将$_REQUEST全部更改为$_POST

      【讨论】:

        猜你喜欢
        • 2020-08-09
        • 1970-01-01
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多