Skip Navigation

[Resolved] Need help in setting limitations on images size and number of images

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 17 replies, has 2 voices.

Last updated by Minesh 11 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2678619

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me where you added the code you shared above?

#2678620

Jay

I have added that code into function.php file

#2678625

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code added to your theme's functions.php file as given under:

add_filter('cred_form_validate','func_validate_image_repeating_field_size',10,2);
function func_validate_image_repeating_field_size($error_fields, $form_data) {
         
      list($fields,$errors)=$error_fields;
       // target form IDs
      $forms = array( 1867 );
         
if (in_array($form_data['id'], $forms ) ) {
       
/// target form field
$form_fields = array("wpcf-crew-member-images");
    
/// allowed size - change as required
$number_of_mb_limit = 2;
$max_size    = $number_of_mb_limit * 1024 *1024;
 
/// allowed number of images - change it as required
$allowed_count = 2;


  foreach($form_fields as $k=>$v):
    
         $count = count($_FILES[$v]['type']);
           
        for($i=0;$i<$count;$i++) {
                      $size = $_FILES[$v]['size'][$i];
            
            if ( ( $size >= $max_size  and   $count > $allowed_count) ) {
                $errors[$v] = "Sorry the file you have uploaded exceeded 2MB limit and upload maximum  $allowed_count files.";
                break;
 
            }else if($size >= $max_size){
                        $errors[$v] = 'Sorry the file you have uploaded exceeded 2MB limit.';
                      break;
            }else if($count > $allowed_count){
                               $errors[$v] = "You are allowed to upload maximum  $allowed_count files.";
                              break;
          }
 
        }
   endforeach;
   
      
 }
return array($fields,$errors);
}

I also suggest with your form:
- hidden link

Under the repeating image field you should add a note that gives information to users that what size is allowed for user and how many files a user can be allowed to upload.