php - displaying error message based on uploading file size not working

Hazirah_Halim

I am making basic uploading file code. User is only allowed to upload two types of files, word and excel, and the file size limit to upload is 2MB.

I try to upload the word file and is 1MB, it has successfully uploaded to the database. When I test the code, the error message based on the incorrect file format is displayed, so it works.

Now here is the problem. When i try to test it by uploading the file 5MB, that is above the limit 2MB, it displayed the message "Contract Form Sucessfully Submitted!", but the file is not uploaded to the database. It works but it displayed the message wrong when it supposed to be "Sorry. Your file is too large. Only 2MB is allowed.".

So what is going on here?

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

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

Check upload_max_filesize and post_max_size in php.ini.and set them something like this

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

then restart your web server and php

you can see them with

<?php
phpinfo();

also

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Displaying an error message in PHP

From Dev

Php File uploading not working

From Dev

File uploader uploading files but displaying error

From Dev

Displaying Message if file size exceeds max limit

From Dev

PHP - Error while uploading file

From Dev

Issue with Displaying error message when the file is empty

From Dev

Error message not displaying html forms using php

From Dev

jquery onchange event not working for file uploading in php

From Dev

File Size Error in php

From Dev

unset of the error message not working in php

From Dev

unset of the error message not working in php

From Dev

PHP file uploading error - UNDEFINED INDEX

From Dev

image not displaying when uploading in php

From Dev

Displaying error message in login page after login failure - If condition is not working

From Dev

No such file or directory - Error Message When Uploading on Open Shift

From Dev

My PHP code is not working when I add HTML code to it ? And this error keep displaying ("Parse error: syntax error, unexpected end of file")?

From Dev

which thing are necessary while uploading large size file php

From Dev

Batch script for checking file size - Error message

From Dev

Why error message is not displaying

From Dev

Uploading Video in php not working

From Dev

PHP image uploading is not working

From Dev

Invalid File error when uploading file with a PHP script

From Dev

Liferay success message for uploading a file

From Dev

LAMP: PHP not working (keeps downloading source file instead of displaying the page)

From Dev

Uploading big size file on server?

From Dev

PHP File Exists not working / Error

From Dev

Error uploading a file in Laravel

From Dev

Error in file uploading code

From Dev

File uploading error in codeception?

Related Related

  1. 1

    Displaying an error message in PHP

  2. 2

    Php File uploading not working

  3. 3

    File uploader uploading files but displaying error

  4. 4

    Displaying Message if file size exceeds max limit

  5. 5

    PHP - Error while uploading file

  6. 6

    Issue with Displaying error message when the file is empty

  7. 7

    Error message not displaying html forms using php

  8. 8

    jquery onchange event not working for file uploading in php

  9. 9

    File Size Error in php

  10. 10

    unset of the error message not working in php

  11. 11

    unset of the error message not working in php

  12. 12

    PHP file uploading error - UNDEFINED INDEX

  13. 13

    image not displaying when uploading in php

  14. 14

    Displaying error message in login page after login failure - If condition is not working

  15. 15

    No such file or directory - Error Message When Uploading on Open Shift

  16. 16

    My PHP code is not working when I add HTML code to it ? And this error keep displaying ("Parse error: syntax error, unexpected end of file")?

  17. 17

    which thing are necessary while uploading large size file php

  18. 18

    Batch script for checking file size - Error message

  19. 19

    Why error message is not displaying

  20. 20

    Uploading Video in php not working

  21. 21

    PHP image uploading is not working

  22. 22

    Invalid File error when uploading file with a PHP script

  23. 23

    Liferay success message for uploading a file

  24. 24

    LAMP: PHP not working (keeps downloading source file instead of displaying the page)

  25. 25

    Uploading big size file on server?

  26. 26

    PHP File Exists not working / Error

  27. 27

    Error uploading a file in Laravel

  28. 28

    Error in file uploading code

  29. 29

    File uploading error in codeception?

HotTag

Archive