0

I am using this code as a mail form where a user can attach a file and it gets emailed. I was wondering what code I can add to limit the file size of the attachments to maybe 5 MB. I am using code from this ShotDev.Com Tutorial. Thanks.

            <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 -->
4

2 回答 2

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)。

于 2013-05-03T16:07:02.903 回答
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.";
}
?>
于 2013-05-03T16:26:35.457 回答