Limit upload size with PHP

Asked

Viewed 2,257 times

1

I set up the php.ini and changed the maximum allowed size for uploading 12MB.

In PHP, I also limited the upload size to 12MB, as follows:

if($_FILES['imagem']['size']>12582912){
   echo "Limite máximo 12 MB!"; 
   //...
}

The problem is if you try to upload a file over 12MB, it shows no error and simply does nothing.

If you change the php.ini and increase the limit to 1GB (for example) and only in the application limit to 12MB, so it already works if you try to send a file with more than 12MB, the error message appears.

Because the error does not appear when I limit everything to 12MB (php.ini and the script) and try to upload with more than 12MB?

1 answer

2


When an uploaded file exceeds the value set in post_max_size, PHP does not fire error, it simply sends a $_POST empty, there is a way to detect if the file exceeds the value set in post_max_size.

if(empty($_FILES) && empty($_POST) 
    && isset($_SERVER['REQUEST_METHOD']) 
    && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){ //pega o erro de tamanho maximo excedido

        $postMax = ini_get('post_max_size'); //pega limite máximo
        echo "Arquivo enviado excede: $postMax"; // exibe o erro
}
else {// continua com o processamento da página

    echo "<pre>";
    print_r($_FILES);
    echo "<pre/>";
}

Solution found in: https://stackoverflow.com/questions/2133652/how-to-gracefully-handle-files-that-exceed-phps-post-max-size

Browser other questions tagged

You are not signed in. Login or sign up in order to post.