Limit upload size with PHP

1

I configured php.ini and changed the maximum allowed size for 12MB upload.

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 larger than 12MB, it does not show the error and simply does nothing.

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

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

    
asked by anonymous 12.06.2014 / 00:42

1 answer

2

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

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

    
12.06.2014 / 02:13