PHP Mailform和文件附件大小限制

时间:2013-05-03 16:01:06

标签: php forms

我将此代码用作邮件表单,用户可以在其中附加文件并通过电子邮件发送。我想知道我可以添加什么代码来将附件的文件大小限制为5 MB。我正在使用ShotDev.Com教程中的代码。感谢。

            <html>
            <head>
            <title>ShotDev.Com Tutorial</title>
            </head>
            <body>
            <?
                $strTo = $_POST["txtTo"];
                    $strTo = "aziola@yahoo.com";
                $strSubject = $_POST["txtSubject"];
                    $strMessage1 = ($_POST["txtrar"]);
                $strMessage2 =  ($_POST["txtDescription"]);
    $strMessage3 =  ($_POST["coname"]);
    $strMessage4 =  ($_POST["address1"]);
    $strMessage5 =  ($_POST["address2"]);

        $strMessage6 =  ($_POST["city"]);

            $strMessage7 =  ($_POST["state"]);

                $strMessage8 =  ($_POST["zip"]);

                    $strMessage9 =  ($_POST["country"]);


    $strMessage10 =  ($_POST["phone"]);

    $strMessage11 =  ($_POST["fax"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";

$strHeader .= $strMessage."\n\n";


$strHeader .= $strMessage1."     \n\n ";

    $strHeader .= $strMessage2."\n\n";

     $strHeader .=  "  <br> <br>  Company Name:  \n\n ";

            $strHeader .= $strMessage3."\n\n";

            $strHeader .=  "  <br> <br>Address1:  \n\n ";

                $strHeader .= $strMessage4."\n\n";

                $strHeader .=  "  <br> <br>Address2:   \n\n ";
                    $strHeader .= $strMessage5."\n\n";

                        $strHeader .=  "  <br> <br>City:   \n\n ";
                        $strHeader .= $strMessage6."\n\n";

                            $strHeader .=  "  <br> <br>State:   \n\n ";
                            $strHeader .= $strMessage7."\n\n";

                                $strHeader .=  "  <br> <br>Zip:   \n\n ";
                                $strHeader .= $strMessage8."\n\n";

                                    $strHeader .=  "<br> <br>Country:   \n\n ";
                                    $strHeader .= $strMessage9."\n\n";

                                $strHeader .=  " <br> <br>Phone:   \n\n ";
                            $strHeader .= $strMessage10."\n\n";
                                    $strHeader .=  " <br> <br>Fax:   \n\n ";
                                $strHeader .= $strMessage11."\n\n"; 




//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 

    $strHeader .= "--".$strSid."\n";
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
    $strHeader .= "Content-Transfer-Encoding: base64\n";
    $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $strHeader .= $strContent."\n\n";
}


$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
    echo "Mail send completed.";
}
else
{
    echo "Cannot send mail.";
}
?>
</body>
</html>
<!--- This file download from www.shotdev.com -->

2 个答案:

答案 0 :(得分:1)

$_FILES["fileAttach"]["size"]应指明附件的大小。您可以添加条件检查以确保它小于限制大小要求。

例如:

$MAX_SIZE = 5242880; // Some arbitrary size (in bytes)

if($_FILES["fileAttach"]["size"] >= $MAX_SIZE) {
    echo "File is too large!";
}

else {
    // Your send code here!
}

值得注意的是,PHP也限制了上传的最大大小(php.ini中的upload_max_filesize)。

答案 1 :(得分:0)

<?php
$maxsize = 2097152; // set you file size here current size: 2 MB
$count = 0; // set your counter here
if($_FILES['uploaded_file']['size'] >= $maxsize){
    //file larger than allowed
    //your rest of the code

} else{

    mail($strTo,$strSubject,null,$strHeader);  
    $count = 1; //send mail and increment counter

}
if($count > 0)
{
    echo "Mail send completed.";
}
else
{
    echo "Cannot send mail.";
}
?>