Answer the question
In order to leave comments, you need to log in
Image size error output via php, if the image size is more than 5 megabytes, how to check?
Through js + ajax, I check the fields for fullness in the form and display an error with php in validation.
Example, this is how I check if a photo is selected or not:
if (empty($_FILES['images']['name'][0])) {
throw new Exception('Выберите фото вид спереди.');
}
$app_config_max_size = 1000000;
$max_size = $app_config_max_size;
if ($_FILES['images']['name'][0]['size'] > $max_size) {
throw new Exception('limit exceeded');
}
Answer the question
In order to leave comments, you need to log in
Never trust what the user sends you. I know that $_FILES can be changed.
There is a function in php, filesize()
Use it to check something like that.
<?php
$max_size = 1024*1024*5; // Один килобайт имеет 1024 байт. Их умножаем и получаем мегабайт. Далее умножаем на пять.
$tmp_size = filesize($_FILES['file_img']['tmp_name'][0]);
if ($tmp_size>$max_size) {
echo "Ошибка, ваш файл привышает 5 мб"; exit();
}
?>
I'm sorry, I found a solution:
$maxFileSize = 5 * 10e6; // = 5 000 000 bytes = 5MB
if ($_FILES['images']['size'][0] > $maxFileSize) {
throw new Exception('Максимальный размер изображения не должен превышать 5 мегабайт, ваша фотография вести больше 5 мегабайт.');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question