php - 显示基于上传文件大小不起作用的错误消息

时间:2017-07-09 10:42:32

标签: php file-upload

我正在制作基本的上传文件代码。用户只能上传两种类型的文件,word和excel,上传的文件大小限制为2MB。

我尝试上传word文件并且是1MB,它已成功上传到数据库。当我测试代码时,会显示基于错误文件格式的错误消息,因此它可以正常工作。

现在问题就在于此。当我尝试通过上传文件5MB(超过限制2MB)来测试它时,它显示消息"合同表格已成功提交!",但该文件未上传到数据库。它可以工作,但是当它应该是"抱歉时它显示错误信息。你的文件太大了。只允许2MB。"。

那么这里发生了什么?

PHP:     

if ($_FILES['upload']['size'] != 0 ){

    $filename = mysqli_real_escape_string($con,$_FILES['upload']['name']);
    $filedata= mysqli_real_escape_string($con,file_get_contents($_FILES['upload']['tmp_name']));
    $filetype = mysqli_real_escape_string($con,$_FILES['upload']['type']);
    $filesize = intval($_FILES['upload']['size']);

    $created = date("Y-m-d H:i:s"); 

    $allowed =  array('zip','rar', 'pdf', 'doc', 'docx');
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    if(in_array($ext, $allowed)){ 

        if($filesize < 2097152) {

            $query = "INSERT INTO contracts( `filename`,`filedata`,`filetype`,`filesize`,`created`) 
                            VALUES (?,?,?,?,?)";
            $stmt = $con->prepare($query);
            $null = NULL;
            $stmt->bind_param("sbsis", $filename, $filedata, $filetype,$filesize,$created);
            $fp = fopen($_FILES['upload']['tmp_name'], "r");

                while (!feof($fp)) {
                $stmt->send_long_data(18, fread($fp,$filesize));
                }
                fclose($fp);
            $stmt->execute();

                if ($stmt->errno){
                    echo "ERROR!!! " . $stmt->error;
                    } else {
                    $successMsg = "Contract Form Sucessfully Submitted!";
                    }
            $stmt->close(); 

        } else {
            $errorMsg = "Sorry. Your file is too large. Only 2MB is allowed.";
        }       
    } else {
        $errorMsg = "Sorry. Only zip, rar, pdf, doc & docx are allowed.";        
    }
} else {
        $created = date("Y-m-d H:i:s"); 
        $query = "INSERT INTO contracts(`created`) 
                        VALUES (?)" ;

        $stmt = $con->prepare($query);
        $stmt->bind_param("s", $created);
        $stmt->execute();

        if ($stmt->errno){
        echo "ERROR!!! " . $stmt->error;

        } else {
       $successMsg = "Contract Form Sucessfully Submitted!";
        }
        $stmt->close();         
    }           
}   

$con->close(); 

?>

HTML表格:

<html>
<body>

<form role="form" method="post" action="" enctype="multipart/form-data">    
                <?php
                if(isset($errorMsg)){
                ?>
                <?php echo $errorMsg; ?>

                <?php
                } else if(isset($successMsg)){
                ?>

                 <?php echo $successMsg; ?>

                <?php
                }
                ?>   

<label>Upload File</label>
                    <input type="file" name="upload" />

                <button type="submit" class="btn btn-primary btn-md" name="submit">
                    <span class="glyphicon glyphicon-plus"></span> Submit New Contract
                    </button>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

检查php.ini中的upload_max_filesizepost_max_size并设置类似这样的内容

memory_limit = 32M
upload_max_filesize = 24M
post_max_size = 32M

然后重启你的web服务器和php

你可以用

看到它们
<?php
phpinfo();

答案 1 :(得分:0)

您是否尝试在文件输入正上方添加这段代码:

<html>
<body>

<form role="form" method="post" action="" enctype="multipart/form-data">    
                <?php
                if(isset($errorMsg)){
                ?>
                <?php echo $errorMsg; ?>

                <?php
                } else if(isset($successMsg)){
                ?>

                 <?php echo $successMsg; ?>

                <?php
                }
                ?>   

<label>Upload File</label>
                    <!-- the added code is here just above the input file type -->
                    <input type="hidden" name="MAX_FILE_SIZE" value="2097152">
                    <input type="file" name="upload" />

                <button type="submit" class="btn btn-primary btn-md" name="submit">
                    <span class="glyphicon glyphicon-plus"></span> Submit New Contract
                    </button>
</form>
</body>

古德勒克!