block upload greater than 2mb with PHP [duplicate]

1

How do I block an upload greater than 2mb?

I'm capturing the file like this:

$imagem = $_FILES["imagem"];
    
asked by anonymous 14.06.2016 / 23:27

1 answer

4

Each $ _FILES key has a size key, which by default is measured in bytes. We can convert this way

for 2mb:

if($_FILES['imagem']['size'] > 2097152) {
    echo 'não é permitido';
}

For 2gb:

if($_FILES['imagem']['size'] > 2147483648) {
    echo 'não é permitido';
}

Note that for 2gb is a large file, you must have the correct settings on the server, php.ini:

post_max_size = 2048M
upload_max_filesize = 2048M
    
14.06.2016 / 23:34