1

I am working on image compression in php.

<?php
if ($_POST) {
 echo $_FILES['file']['size'];
}
?>
<html>
    <head><title>Php code compress the image</title></head>
    <body>
  <form action="" name="myform" id="myform" method="post" enctype="multipart/form-data">
   <ul>
    <li>
     <label>Upload:</label>
                    <input type="file" name="file" id="file"/>
    </li>
    <li>
     <input type="submit" name="submit" id="submit" class="submit btn-success"/>
    </li>
   </ul>
  </form>
 </body>
</html>

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 button

Paramesh Reddy
  • 325
  • 1
  • 3
  • 14
  • to earase the max upload size take a look here http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size (i thought this is what you want) otherwise check the filesize in $_FILES ... – donald123 Dec 19 '14 at 14:22

4 Answers4

3

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
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
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
} 
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

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

Sven van de Scheur
  • 1,809
  • 2
  • 15
  • 31
0

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.

anildsvv
  • 1
  • 3