Skip to content Skip to sidebar Skip to footer

How To Check Uploaded Image Size Is Greater Than 2MB In Php?

I am working on image compression in php. This is working fine with the images less than 2MB. if size>2MB then even its not showing in $_FILES after clicking on the submit butt

Solution 1:

You first problem is that you're not checking if an upload was successful. You cannot use ANYTHING in $_FILES until you've checked for that error. And checking for $_POST is not "error checking".

At bare minimum you should have

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
        die("Upload failed with error code " . $_FILES['file']['error']);
   }
   ... got here, upload was successful
}

Solution 2:

You can check $_FILES['file']['error'] and check if it's value is equal to the magic constant UPLOAD_ERR_INI_SIZE

if ($_FILES['file']['error'] === UPLOAD_ERR_INI_SIZE) { 
    //uploading failed due to size limmit
} 

Solution 3:

This is probably due to the "upload_max_filesize" and/or "max_post_size" setting. Check for the setting in your php.ini.


Solution 4:

You can use this if the uploaded size is greater than 2 mb it shows an error

if($file_size > 2048) {
    $errors[]='File size is greater than 2 MB';
}

Or if you want to upload image size more than 2 mb then edit your php.ini file and change "upload_max_filesize" and/or "max_post_size" value.


Post a Comment for "How To Check Uploaded Image Size Is Greater Than 2MB In Php?"