0

我的表单上有一个输入,允许用户浏览到文件位置。这个想法是他们可以将简历附加到他们准备提交的申请中。

<label class="description" for="element_5">Upload a File </label>
        <div>
            <input id="element_5" name="element_5" class="element file" type="file"/> 

对于我的文本字段和下拉列表,我一直在使用以下内容:

$experince = $_POST["experince"];

但我不想要我想要实际文件的路径字符串。如何获取文件本身以及如何将其附加到电子邮件中?

此外,是否有一种简单的方法可以将附件限制为 .DOC / .PDF?

4

4 回答 4

3

我最近做了类似的事情。这不是微不足道的。

您的电子邮件消息必须是多部分的 Mime 消息。您将读入您的文件,对其进行编码(使用 base64),然后将其放入您的电子邮件消息字符串中的正确位置。

这看起来像一个不错的教程(我希望我以前找到过): http ://www.texelate.co.uk/blog/send-email-attachment-with-php/

但请注意,本教程存在一些转义问题:

$message . "“nn";

应该:

$message . "\n\n";

您还可以查看 Mail_Mime PEAR 包寻求帮助: http ://pear.php.net/package/Mail_Mime

于 2009-10-16T14:37:21.407 回答
3

看一眼

于 2009-10-16T14:37:35.443 回答
0

这是我发送带有附件的邮件的操作。有些代码是我的,有些来自http://php.net/manual/en/function.mail.php。这段代码已经过试验和测试,所以它应该可以工作。还要查看是否安装了 sendmail。如果在 linux ubuntu 系统上尝试 sudo apt-get install sendmail 来安装它。这段代码最好的部分是它适用于多个文件上传。

文件名:index.php

<?php
if( $_POST || $_FILES )
{
    // email fields: to, from, subject, and so on
    // Here 
    $from = "someone@somewhere.com";
    $to = "someone_else@somewhere_else.com";
    $subject = "Mail with Attachment";
    $message = "This is the message body and to it I will append the attachments.";
    $headers = "From: $from";

    // boundary
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    // headers for attachment
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

    // multipart boundary
    $message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    fixFilesArray($_FILES['attachment']);
    foreach ($_FILES['attachment'] as $position => $file) 
    {
        // should output array with indices name, type, tmp_name, error, size
        $message .= "--{$mime_boundary}\n";
        $fp     = @fopen($file['tmp_name'],"rb");
        $data   = @fread($fp,filesize($file['tmp_name']));
        @fclose($fp);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    }
    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $from;
    $ok = @mail($to, $subject, $message, $headers, $returnpath);
    if($ok){ return 1; } else { return 0; }
}
//This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important

function fixFilesArray(&$files)
{
    $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);

    foreach ($files as $key => $part) {
        // only deal with valid keys and multiple files
        $key = (string) $key;
        if (isset($names[$key]) && is_array($part)) {
            foreach ($part as $position => $value) {
                    $files[$position][$key] = $value;
            }
            // remove old key reference
            unset($files[$key]);
        }
    }
}
?>
<html>
    <body>
        <form method="POST" action="index.php" enctype="multipart/form-data">
            <input type="file" name="attachment[]"><br/>
            <input type="submit">
        </form>
    </body>
</html>

顺便说一句,我想为使用 sendmail 道歉,因为它有点慢。我会尝试发布更好的解决方案。

于 2011-12-08T08:06:55.017 回答
-1

这是我到目前为止所拥有的,但它不起作用

<input id="resumeup" name="resumeup"  type="file"/> 

<?php $_FILES = $_POST["resumeup"];

            if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/pjpeg"))
            && ($_FILES["file"]["size"] < 20000))
            {
                if ($_FILES["file"]["error"] > 0)
                {
                    echo "Error: " . $_FILES["file"]["error"] . "<br />";
                }
                else
                {
                    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                    echo "Type: " . $_FILES["file"]["type"] . "<br />";
                    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                    echo "Stored in: " . $_FILES["file"]["tmp_name"];
                }
            } ?>
于 2009-10-19T20:05:31.950 回答