Skip Navigation

[Resolved] File upload size limit doesn’t seem to be working

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to validate the uploaded file size value

Solution:
You can use CRED hook 'cred_form_ajax_upload_validate' to validate the uploaded file size

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/file-upload-size-limit-doesnt-seem-to-be-working/#post-648449

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

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 8 replies, has 2 voices.

Last updated by gavinS-2 6 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#647870

I am trying to: create a file upload size limit as per this ticket: https://toolset.com/forums/topic/types-file-custom-field-and-cred/

Link to a page where the issue can be seen: hidden link

I expected to see: I expected the form to limit the size of images, but this does not seem to be happening. Can you see any problems with my code:

add_filter('cred_form_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($field_data, $form_data)
{
    // Field data are field values and errors 
    list($fields,$errors)=$field_data;
  
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-logo-profile-pic']['type'])) && (isset($_FILES['wpcf-logo-profile-pic']['size']))) {
  
        //Retrieve file type
        $file_type_uploaded=$_FILES['wpcf-logo-profile-pic']['type'];
         
        //Retrieve file size
        $file_size_uploaded=$_FILES['wpcf-logo-profile-pic']['size'];
         
        //Validate files uploaded, make sure its jpg file type AND not more than 50kB
        if (!(  ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 51200) )) {
             
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['logo-profile-pic']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (50Kb).';   
                 
        } 
  
    }
	
	//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-picture-1']['type'])) && (isset($_FILES['wpcf-picture-1']['size']))) {
  
        //Retrieve file type
        $file_type_uploaded=$_FILES['wpcf-picture-1']['type'];
         
        //Retrieve file size
        $file_size_uploaded=$_FILES['wpcf-picture-1']['size'];
         
        //Validate files uploaded, make sure its jpg file type AND not more than 500kB
        if (!(  ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 512000) )) {
             
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['picture-1']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (500Kb).';   
                 
        } 
  
    }
	
	//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-picture-2']['type'])) && (isset($_FILES['wpcf-picture-2']['size']))) {
  
        //Retrieve file type
        $file_type_uploaded=$_FILES['wpcf-picture-2']['type'];
         
        //Retrieve file size
        $file_size_uploaded=$_FILES['wpcf-picture-2']['size'];
         
        //Validate files uploaded, make sure its jpg file type AND not more than 500kB
        if (!(  ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 512000) )) {
             
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['picture-2']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (500Kb).';   
                 
        } 
  
    }
	
	//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-picture-3']['type'])) && (isset($_FILES['wpcf-picture-3']['size']))) {
  
        //Retrieve file type
        $file_type_uploaded=$_FILES['wpcf-picture-3']['type'];
         
        //Retrieve file size
        $file_size_uploaded=$_FILES['wpcf-picture-3']['size'];
         
        //Validate files uploaded, make sure its jpg file type AND not more than 500kB
        if (!(  ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 512000) )) {
             
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['picture-3']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (500Kb).';   
                 
        } 
  
    }
	
    //return result
    return array($fields,$errors);
}
#647882

Ooh, I see that the filetype should probably have been image/jpg not application/jpg, but the error messages are still not working?

#648159

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

CRED now upload the file using AJAX so there is different hook to validate the file size: cred_form_ajax_upload_validate

For example:

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form
    if ($form_data['id']==9999)
    {
        if ($fields['_featured_image']['field_data']['size'] > 1000000)
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Wrong size image';
        }
    }
    //return result
    return array($fields,$errors);
}

Where:
- Replace 9999 with your CRED form ID.

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

#648410

Hi Minesh

This doesn't seem to be working for me:


add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form
    if ($form_data['id']==497)
    {
        if ($fields['logo-profile-pic']['field_data']['size'] > 51200)
        {
            //set error message for featured image
            $errors['logo-profile-pic'] = 'Profile Pic too large (>50Kb)';
        }
    }
    //return result
    return array($fields,$errors);
}

I've changed the Form ID and the field name to logo-profile-pic. Can you see anything I've done wrong?

#648449

Minesh
Supporter

Languages: English (English )

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

Could you please try following code: I've added prefix "wpcf-" to your custom field name.

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form
    if ($form_data['id']==497)
    {
        if ($fields['wpcf-logo-profile-pic']['field_data']['size'] > 51200)
        {
            //set error message for featured image
            $errors['wpcf-logo-profile-pic'] = 'Profile Pic too large (>50Kb)';
        }
    }
    //return result
    return array($fields,$errors);
}
#648523

Yay! That seems to work. Thanks so much Minesh

#648530

Hi Minesh

Oh, one more thing. The error message that pops up includes the name of the field ie:

wpcf-logo-profile-pic: Profile Pic too large (>50Kb)

Is there a way to remove the field name so it just says:

Profile Pic too large (>50Kb)

#648554

Minesh
Supporter

Languages: English (English )

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

There is no feature available to display only error message but I'll pass your concern to our feature request team and send it as a new feature request for assessment.

#652085

Thanks Minesh