0

I've been wondering why is it that i got no error when i try to upload file with size more than 2mb?

I already read this How to limit file upload type file size in PHP? but still not working. (Note: i'm not working on client side validation here like what it did using javascript).

Here's my code:

<form action="" method="post" enctype="multipart/form-data">
                        <input type="text" name="title" class="form-control" placeholder="Document Title" required autofocus><br>
                        <textarea name="description" class="form-control" placeholder="Description" rows="10" cols="10"></textarea><br>
                        <input type="file" name="documento" required><br>
                        <button type="upload" name="upload" class="btn btn-default"><span class="glyphicon glyphicon-open"> </span> Upload</button>
                    </form>

Then

if (isset($_POST['upload'])) {
    $ddd_t = htmlspecialchars($_POST['title']);
    $ddd_d = htmlspecialchars($_POST['description']);
    date_default_timezone_set('Asia/Manila');
    $date_time = date('Y-m-d H:i:s') ;
    $filename = strtolower($_FILES['documento']['name']);
    $target = "../downloads/files/";
    $rand = rand(1,10000);
    $target = $target . $rand . "_" . $filename;
    $maxsize = 2097152;
    $mime_type = array(
        'application/pdf',
        'application/vnd.ms-powerpoint',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'application/vnd.ms-excel',
        'text/plain',
        'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        'application/msword'
        );
    if (empty($ddd_t)) {
        $errorize1 = "Required Title!";
    }
    else{
        if (empty($filename)) {
            $errorize1 = "Required File!";
        }
    }
    if (($_FILES['documento']['size'] > $maxsize) || ($_FILES['documento']['size'] == 0)){
        $errorize = "Max size is 2mb";
    }
    else{
        if (!in_array($_FILES['documento']['type'], $mime_type)){
            $errorize = "Invalid File. Only powerpoint, excel, pdf, word, plain-txt accepted!";
        }
    }
    if (isset($errorize1) || isset($errorize))
    {

    }
    else{
    $_FILES['documento']['name'] = $rand . "_" . $filename;
    $upload_file = ($_FILES['documento']['name']);
    $query = "INSERT INTO files(docu_title, description, link, date) VALUES(:ddd_t, :ddd_d, :document, :date_time)";
    $data = $conn->prepare($query);
    $result = $data->execute(array(':ddd_t' => $ddd_t, ':ddd_d' => $ddd_d, ':document' => $upload_file, ':date_time' => $date_time));
    $move = move_uploaded_file($_FILES['documento']['tmp_name'], $target);
    if ($result && $move) {
        $upload_img = header("location:?success=true&file=".$_FILES['documento']['name']);
    }
    else{
        echo "error!";
    }
    }
}

And

<?php
            if (isset($errorize)) {
                echo '<div class="alert alert-danger alert-dismissable">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize .'</div>';
            }
            if (isset($errorize1)) {
                echo '<div class="alert alert-danger alert-dismissable">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize1 .'</div>';
            }
            ?>

Only the invalid mime type here is working.. I don't know what's wrong with the file size error when i try to upload more than 2mb.

15
  • check out php ini and see what is the upload_max_filesize and its 2MB by default. You need to change that to allow upload more than 2 MB Commented Feb 9, 2014 at 17:18
  • I can see 2M in the upload_max_filesize .. no, i won't change that.. i just need to warn user if he/she uploaded more than 2mb. that is why i made my own custom error message $errorize .. Commented Feb 9, 2014 at 17:22
  • Sidenote: This $upload_img = header("location:?success=true&file=".$_FILES['document']['name']); should probably be $upload_img = header("location:?success=true&file=".$_FILES['documento']['name']); yet it won't fix your problem, just pointing something out. Commented Feb 9, 2014 at 17:26
  • yes, sorry.. i already change that :) but nothing works Commented Feb 9, 2014 at 17:27
  • You also could change the limit size using .htaccess if you can't modify your php.ini file. That's usually the fix. @user3258603 Commented Feb 9, 2014 at 17:28

2 Answers 2

0

in php.ini check this

upload_max_filesize
max_input_time
memory_limit
max_execution_time
post_max_size

upload_max_filesize = 2M` please increase the limit 

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M  do it as you want 
Sign up to request clarification or add additional context in comments.

Comments

0

I think your code placement may be the issue here.

Upon testing the following, the error messages did appear correctly if a document was either too large or was not the right type.

The following is to be inside ONE file.

<?php

if (isset($_POST['upload'])) {
    $ddd_t = htmlspecialchars($_POST['title']);
    $ddd_d = htmlspecialchars($_POST['description']);
    date_default_timezone_set('Asia/Manila');
    $date_time = date('Y-m-d H:i:s') ;
    $filename = strtolower($_FILES['documento']['name']);
    $target = "../downloads/files/";
    $rand = rand(1,10000);
    $target = $target . $rand . "_" . $filename;
    $maxsize = 2097152;

    $mime_type = array(
        'application/pdf',
        'application/vnd.ms-powerpoint',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'application/vnd.ms-excel',
        'text/plain',
        'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        'application/msword'
        );
    if (empty($ddd_t)) {
        $errorize1 = "Required Title!";
    }
    else{
        if (empty($filename)) {
            $errorize1 = "Required File!";
        }
    }
    if (($_FILES['documento']['size'] > $maxsize) || ($_FILES['documento']['size'] == 0)){
        $errorize = "Max size is 2mb";
    }
    else{
        if (!in_array($_FILES['documento']['type'], $mime_type)){
            $errorize = "Invalid File. Only powerpoint, excel, pdf, word, plain-txt accepted!";
        }
    }
    if (isset($errorize1) || isset($errorize))
    {

    }
    else{
    $_FILES['documento']['name'] = $rand . "_" . $filename;
    $upload_file = ($_FILES['documento']['name']);
    $query = "INSERT INTO files(docu_title, description, link, date) VALUES(:ddd_t, :ddd_d, :document, :date_time)";
    $data = $conn->prepare($query);
    $result = $data->execute(array(':ddd_t' => $ddd_t, ':ddd_d' => $ddd_d, ':document' => $upload_file, ':date_time' => $date_time));
    $move = move_uploaded_file($_FILES['documento']['tmp_name'], $target);
    if ($result && $move) {
         $upload_img = header("location:?success=true&file=".$_FILES['documento']['name']);
    }
    else{
        echo "error!";
    }
    }
}

?>

<?php
if (isset($errorize)) {
    echo '<div class="alert alert-danger alert-dismissable">
              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize .'</div>';
}
if (isset($errorize1)) {
    echo '<div class="alert alert-danger alert-dismissable">
              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize1 .'</div>';
}
?>

<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="title" class="form-control" placeholder="Document Title" required autofocus><br>
<textarea name="description" class="form-control" placeholder="Description" rows="10" cols="10"></textarea><br>
<input type="file" name="documento" required><br>
<button type="upload" name="upload" class="btn btn-default"><span class="glyphicon glyphicon-open"> </span> Upload</button>
</form>

8 Comments

Actually, in that way i wrote the code .. but still it doesn't work.. i'll try to paste my whole code for this page in pastebin..
That's strange. Are you using any other code in conjunction with it? @user3258603
And what are the exact steps that you took? I.e.: 1) Choose a file larger than accepted. 2) Choose a different filetype. @user3258603 that is what I did, and worked for both instances.
Yes Sir.. that's exactly i am doing here.. i really don't know whats wrong with my code.. BTW, here is the entire code pastebin.com/efmsG0n1
In your pastebin you have if (isset($errorize1)) and then show </button>'. $errorize .'</div>'; notice the 1 in $errorize1 but not in $errorize ? Then you have if (isset($errorize1)) { using </button>'. $errorize1 .'</div>'; - both are using $errorize1 so if (isset($errorize1)) { being the first one, should be if (isset($errorize)) { --- Plus, it might also be a mix of something else, being the CSS styles associated with it. @user3258603
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.