Validate large file in server side

Discussion in 'PHP' started by piropeator, Aug 11, 2018.

  1. #1
    Hello! I have a simple code to upload a file to server.

    <!DOCTYPE html>
    <html lang="es">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <form action="upload3.php" enctype="multipart/form-data" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
            Upload File: <input name="file_upload" type="file" />
            <input type="submit" value="Enviar fichero" />
        </form>
    </body>
    </html>
    HTML:
    In PHP:
    <?php
    define('APP_BASEDIR', dirname(__FILE__));
    $ruta = APP_BASEDIR . "/cargas/";
    
    $file_uploaded = $ruta. basename($_FILES['file_upload']['name']);
    
    if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $file_uploaded)) {
        echo "OK";
    } else {
        echo "ERROR!";
    }
    ?>
    PHP:
    If the file is small, there is no problem.
    But if the file is very large, it fails.
    Show this:
    And tell Notice: Undefined index... this because $_FILE are empty (see http://php.net/manual/en/ini.core.php#ini.post-max-size)
    I don't need to upload a large file. I just want to validate on the server side that a large file is not loaded (2Mb Max).
    Can someone help me?
     
    piropeator, Aug 11, 2018 IP
  2. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #2
    RoseHosting, Aug 11, 2018 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Its a variable in php.ini which is making the script fail. Increase its limit and script will continue as normal.

    ; Maximum size of POST data that PHP will accept.
    post_max_size = 8M


    Make this:
    post_max_size = 18M
    or
    post_max_size = 25M
    or so on... Whatever is needed...
     
    JEET, Sep 17, 2018 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    This one also needs to be increased.

    ; Maximum allowed size for uploaded files.
    upload_max_filesize = 2M
     
    JEET, Sep 17, 2018 IP