带附件的联系表单的PHP文件大小验证

时间:2017-01-21 16:39:57

标签: php filesize

我已经与php建立了联系表单,对名称,电子邮件和邮件的验证效果很好,但我的文件大小验证不起作用。当我尝试上传大于2 MB的文件时,它会加载并崩溃。 我已经没有想法该怎么做了。谁能帮我? 这是我的php代码:

<?php
    if ($_POST["submit"]) {

        if (!$_POST['name']){
            $error="Unesite svoje ime.";
        }
        if (!$_POST['email']) {
            $error.="Unesite e-mail adresu.";
        }
        if (!$_POST['message']) {
            $error.="Unesite tekst poruke";
        }
        if ($_POST['email']!="" AND !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
            $error.="Unesite validu e-mail adresu.";
        }

        if(!empty($_FILES['my-file']['name'])){
            $file_name = $_FILES['my-file']['name'];
            $temp_name = $_FILES['my-file']['tmp_name'];
            $file_type = $_FILES['my-file']['type'];

            if($_FILES['my-file']['size'] > 524288){
                $error.="Maksimalna velicina fajla je 5 MB.";
            } else {

            $from = $_POST['email'];
            $to = "nicolletta.dj@gmail.com";
            $subject = "Poruka sa sajta";
            $message = "Tekst poruke:".$_POST['message'];

            $file = $temp_name;
            $content = chunk_split(base64_encode(file_get_contents($file)));
            $uid = md5(uniqid(time()));

            $header = "MIME-Version: 1.0\r\n"; 
            $header .= "From:".$from."\r\n"; 
            $header .= "Reply-To: ".$_POST['email']."" . "\r\n";
            $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
            $header .= "This is a multi-part message in MIME format.\r\n";

            //plain text part
            $header .= "--".$uid."\r\n";
            $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
            $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
            $header .= $message."\r\n\r\n";
            //attachment part
            $header .= "--".$uid."\r\n";
            $header .= "Content-type: ".$file_type."; name=\"".$file_name."\"\r\n";
            $header .= "Content-Transfer-Encoding: base64\r\n";
            $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
            $header .= $content."\r\n\r\n";

            if ($error) {
                $result='<div class="alert alert-danger">'.$error.'</div>';
            } else {
                if (mail($to, $subject, "", $header)) {
                $result='<div class="alert alert-success"><strong>Vasa poruka je poslata!</strong></div>';} 
                else {
                $result='<div class="alert alert-danger">Došlo je do greške prilikom slanja poruke. Molim Vas pokušajte ponovo kasnije.</div>';}
    } //if there is no error
        }} else {if ($error) {
                $result='<div class="alert alert-danger">'.$error.'</div>';
            } else {
                $header = "From:".$_POST['email']."\r\n";
                $header = "Reply-To: ".$_POST['email']. "\n" ;
                $body = "Tekst poruke:".$_POST['message'];

                if (mail('nicolletta.dj@gmail.com', 'Poruka sa sajta', $body, $header)) {
                $result='<div class="alert alert-success"><strong>Vasa poruka je poslata!</strong></div>';} 
                else {
                $result='<div class="alert alert-danger">Došlo je do greške prilikom slanja poruke. Molim Vas pokušajte ponovo kasnije.</div>';}
    }}//if files not empty
    } //if is submited
?>

和我的HTML代码:

<div class="col-md-6 emailForm">
    <?php echo $result; ?>
    <form id="contact_body" method="post" action="index.php" enctype="multipart/form-data">

        <div class="form-group">
            <label for="name">Ime i prezime:</label>
            <input type="text" name="name" class="form-control" placeholder="Ime i prezime" data-required="true"/>
        </div>

        <div class="form-group">
            <label for="email">E-mail:</label>
            <input type="text" name="email" class="form-control" placeholder="E-mail" data-required="true"/>
        </div>

        <div class="form-group">
            <label for="comment">Poruka:</label>
            <textarea class="form-control" name="message" data-required="true"></textarea>
        </div>

        <div class="form-group">
            <label class="btn btn-primary">
                <input type="file" name="my-file" style="display:none;" onchange="$('#upload-file-info').html(this.files[0].name);">
                    Dodajte sliku
            </label>
            <span class='label label-info' id="upload-file-info"></span>
        </div>
        <div id="push" class="pushbutton"></div>
        <input type="submit" name="submit" class="btn btn-lg" value="Poslati"/>         
    </form>
</div>

2 个答案:

答案 0 :(得分:0)

您可能会收到错误 - 因为您为php上传设置设置了较低的值:

上传文件的最大允许大小。

upload_max_filesize = 40M

必须大于或等于upload_max_filesize

post_max_size = 40M

如果达到此限制 - 文件未上传且 $ _ FILES [..] [错误]!= 0。

第二个 - 524288不是5MB但只有524kB

答案 1 :(得分:0)

添加零

if($_FILES['my-file']['size'] > 5242880)