带有文件附件的表单以及脚本

时间:2017-01-28 17:17:26

标签: php forms file upload attachment

我不能为我的生活弄清楚我做错了什么。我小脑袋里的剧本似乎还可以。如果删除部分脚本,我可以在没有文件验证的情况下使用它。请问有人可以点亮吗?我是新手。

HTML部分:

    <form enctype="multipart/form-data" action="process.php" method="post">
    <p><label for="file">Select file to upload</label> <input type="file" name="file" id="file"></p>
    <p><input type="submit" name="submit" id="submit" value="send"></p>
    </form>

process.php

<?php
if(isset($_POST['submit'], $_FILES['file'])) {
$errors = array();
$maxsize = 4096000;
$acceptable = array(
    'application/pdf',
    'msword/doc',
    'msword/docx',
    'text/plain'
);

if(($_FILES['file']['size'] >= $maxsize) || ($_FILES["file"]["size"] == 0)) {
    $errors[] = 'File too large. File must be less than 4 megabytes.';
}

if(!in_array($_FILES['file']['type'], $acceptable)) && (!empty($_FILES["file"]["type"])) {
    $errors[] = 'Invalid file type. Only pdf, doc, docx and txt files are accepted.';
}

if(count($errors) === 0)

{

    $to = 'test@hotmail.com';
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
    $filename = $_FILES['file']['name'];

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

    $headers = "From: info@example.com\r\nReply-To: info@example";
    $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=\"utf-8\"
Content-Transfer-Encoding: 8bit

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

else {
    foreach($errors as $error) {
        echo '<script>alert("'.$error.'");</script>';
    }

    die(); //Ensure no more processing is done
}
}
?>

0 个答案:

没有答案