Title: Minimum Upload Size, please!
Last modified: August 31, 2016

---

# Minimum Upload Size, please!

 *  Resolved [v123shine](https://wordpress.org/support/users/v123shine/)
 * (@v123shine)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/)
 * Good day,
 * Scenario: One of my author always upload images less than 10 kb. This image is
   very low quality. I’m tired to tell him for upload images more bigger size. When
   I search in google, i found this code (and work for me):
 *     ```
       add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );
   
       function wpse_28359_block_authors_from_uploading_small_images()
       {
           if( !current_user_can( 'administrator') )
               add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' );
       }
   
       function wpse_28359_block_small_images_upload( $file )
       {
           // Mime type with dimensions, check to exit earlier
           $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );
   
           if( !in_array( $file['type'], $mimes ) )
               return $file;
   
           $img = getimagesize( $file['tmp_name'] );
           $minimum = array( 'width' => 640, 'height' => 480 );
   
           if ( $img[0] < $minimum['width'] )
               $file['error'] =
                   'Image too small. Minimum width is '
                   . $minimum['width']
                   . 'px. Uploaded image width is '
                   . $img[0] . 'px';
   
           elseif ( $img[1] < $minimum['height'] )
               $file['error'] =
                   'Image too small. Minimum height is '
                   . $minimum['height']
                   . 'px. Uploaded image height is '
                   . $img[1] . 'px';
   
           return $file;
       }
       ```
   
 * My question is: How to make that code to prevent images upload less than 20 kb.
   Thank you very much.
 * Kind regards,
    Shine

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [mcfreder](https://wordpress.org/support/users/mcfreder/)
 * (@mcfreder)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/#post-7007914)
 * Hello,
 * I have edited your code on the fly, not sure how it will render, but you can 
   try:
 *     ```
       add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );
   
       function wpse_28359_block_authors_from_uploading_small_images()
       {
           if( !current_user_can( 'administrator') )
               add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' );
       }
   
       function wpse_28359_block_small_images_upload( $file )
       {
           // Mime type with dimensions, check to exit earlier
           $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );
   
           if( !in_array( $file['type'], $mimes ) )
               return $file;
   
           $img = filesize( $file['tmp_name'] );
           $minimum = "20000";
       	$minimumsize = "20Kb";
   
           if ( $img < $minimum )
               $file['error'] =
                   'Image too small. Minimum size is '
                   . $minimumsize
                   . '. Uploaded again with the required filesize ';
   
           return $file;
       }
       ```
   
 * The idea is to use filesize() instead of getimagesize()
 *  [Phil Erb](https://wordpress.org/support/users/philerb/)
 * (@philerb)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/#post-7007953)
 * This code is checking for particular image dimensions (it’s currently configured
   for 640×480) – this may be a better method than file size, because a file with
   a larger size could still be smaller than the dimensions that you would really
   like.
 * With that said, the PHP function that would get the file size (in bytes) of the
   picture is:
 * filesize ( $file[‘tmp_name’] )
 * Checking the size of that against your minimum size – similar to how that function
   is checking the width and height – should do the trick.
 *  [Phil Erb](https://wordpress.org/support/users/philerb/)
 * (@philerb)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/#post-7007955)
 * This code is untested, but I’ve replaced the dimension check from the example
   above with a file size check:
 *     ```
       <?php
       add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );
   
       function wpse_28359_block_authors_from_uploading_small_images()
       {
           if( !current_user_can( 'administrator') )
               add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' );
       }
   
       function wpse_28359_block_small_images_upload( $file )
       {
           // Mime type with dimensions, check to exit earlier
           $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );
   
           if( !in_array( $file['type'], $mimes ) )
               return $file;
   
           $minfilesize = 20000; //Minimum file size in bytes
       	$imgfilesize = filesize ( $file['tmp_name'] );
   
       	if ( $imgfilesize < 20000 )
       		$file['error'] =
                   'Image too small. Minimum file size is '
       			. $minfilesize
       			. ' bytes. The uploaded file is '
       			. $imgfilesize
       			. ' bytes.';
   
           return $file;
       }
       ?>
       ```
   
 *  [Rudá Almeida](https://wordpress.org/support/users/rudaalmeida/)
 * (@rudaalmeida)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/#post-7007957)
 * Hey v123shine,
 * the code you posted checks for the image dimensions (width and heigth). I’ve 
   added another check for the filesize.
 * I have tested it on a local WP install, please let me know if it doesn’t work!
 *     ```
       add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );
   
       function wpse_28359_block_authors_from_uploading_small_images()
       {
           if( !current_user_can( 'administrator') )
               add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' );
       }
   
       function wpse_28359_block_small_images_upload( $file )
       {
           // Mime type with dimensions, check to exit earlier
           $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );
   
           if( !in_array( $file['type'], $mimes ) )
               return $file;
   
           $img = getimagesize( $file['tmp_name'] );
           $minimum = array( 'width' => 640, 'height' => 480 );
   
           if ( $img[0] < $minimum['width'] )
               $file['error'] =
                   'Image too small. Minimum width is '
                   . $minimum['width']
                   . 'px. Uploaded image width is '
                   . $img[0] . 'px';
   
           elseif ( $img[1] < $minimum['height'] )
               $file['error'] =
                   'Image too small. Minimum height is '
                   . $minimum['height']
                   . 'px. Uploaded image height is '
                   . $img[1] . 'px';
   
             $imgsize = filesize($file['tmp_name']);
       	$minsize = 800; // Minimun file size in KB
             if ( $imgsize < (1024 * $minsize) )
               $file['error'] =
                   'File size too small. Minimum size is '
                   . $minsize
                   . 'kb. Uploaded file size is '
                   . sprintf('%0.2f', ($imgsize /1024)) . ' kb';
   
           return $file;
       }
       ```
   
 *  [Rudá Almeida](https://wordpress.org/support/users/rudaalmeida/)
 * (@rudaalmeida)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/#post-7007958)
 * Damn, beat by a minute =/
 *  Thread Starter [v123shine](https://wordpress.org/support/users/v123shine/)
 * (@v123shine)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/#post-7007995)
 * Thank you very much Mcfreder, Phil Erb and Ruda Almeida.
 * Problem solved 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Minimum Upload Size, please!’ is closed to new replies.

## Tags

 * [20q2016](https://wordpress.org/support/topic-tag/20q2016/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 4 participants
 * Last reply from: [v123shine](https://wordpress.org/support/users/v123shine/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/minimum-upload-size-please/#post-7007995)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
