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

2 Answers2

1

$_FILES["fileAttach"]["size"] Should indicate the size of your attachment. You can add a conditional check to ensure that it is less than your limiting size requirements.

For example:

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

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

else {
    // Your send code here!
}

It may also be important to note that PHP limits the max size of uploads as well (upload_max_filesize in php.ini).

Julio
  • 2,261
  • 4
  • 30
  • 56
  • You can also add an HTML element to your form specifying the max size but it doesn't evaluate it until the entire file is uploaded so should only use as a failsafe http://stackoverflow.com/questions/6327965/html-upload-max-file-size-does-not-appear-to-work – Brock Hensley May 03 '13 at 16:18
  • 1
    just an FYI: the file size in $_FILES["fileAttach"]["size"] will be in bytes – Software Guy May 03 '13 at 16:22
  • Why too large if($_FILES["fileAttach"]["size"] < $MAX_SIZE) you might be making mistake Louis.. – Vijaya Pandey May 03 '13 at 16:28
  • and $_FILES['uploaded_file']['size'] gives size in bytes, you are making slight mistake. See my answer please – Vijaya Pandey May 03 '13 at 16:29
  • instead of just having this: echo "File is too large!"; how can I stop the form from being sent? – user2347566 May 03 '13 at 16:49
  • @user2347566 see the added else. Place your send code inside of that block - it'll only execute of the file size is good. Otherwise you can exit() on the "too large" error. I'd go with the former, however. – Julio May 03 '13 at 16:52
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.";
}
?>
Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57