Skip Navigation

[Resolved] Limit File Type & File Size in Toolset Forms Image Field

This support ticket is created 3 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 6 replies, has 3 voices.

Last updated by Clayton Chase 3 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2111097

Hey guys,

I'm using Toolset Forms to allow users to create a listing. I need to limit the number of files, the file size, and the file type. I'm using the Toolset "Image" field type with the "Allow multiple instances of this field" setting.

I've searched and found several tickets with the same problem but haven't been able to get any of them to work.

Here is the code I tried:
https://pastebin.com/3i7aTgRm

How can I make this happen?

#2111331

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting Toolset support.

The code in your screenshot won't work for a field that accepts multiple instances. It will work for an Image field that accepts only one value. Please check the code in this reply https://toolset.com/forums/topic/validate-the-image-field-repeating-in-the-post-form-in-toolset/#post-1892673

Let me know if you have further quesitons.

#2112391

Hey Jamal,

I've done my best to modify the code but it still doesn't seem to be working. My code is pasted below.

add_action('cred_form_validate', 'my_image_validation_func',10,2);
function my_image_validation_func($error_fields, $form_data) {
    // all the valid file types
    $file_types = array('image/jpeg','image/png','image/jpg');
    $max_size = 1 * 1024 * 1024;
    $forms = array( 358 );
 
    // Field data are field values and errors
    list($fields,$errors)=$error_fields;
 
    if (in_array($form_data['id'], $forms ) && (isset($_FILES['wpcf-images']['type']))) {
        // 1. Max number of photos is '6'.
        if ( count( $_FILES['wpcf-images']['name'] ) > 6 ) {
            $errors['wpcf-images'] = 'You may only add up to 6 images.';
            return array( $fields, $errors );
        }
 
        // 2. Dimensions of the photo should be at least 350 (height) x 350 (width).
        foreach ( $_FILES['wpcf-images']['tmp_name'] as $tmp_image ) {
            $sizes = getimagesize( $tmp_image );
            if ( $sizes[0] < 350 || $sizes[1] < 350 ) {
                $errors['wpcf-images'] = 'An image is too small. Images should be at least 350x350';
                return array( $fields, $errors );
            }
        }
 
        // 3. The file types should be JPG, PNG and JPEG.
        foreach ( $_FILES['wpcf-images']['type'] as $type ) {
            if ( !in_array( $type, $file_types ) ) {
                $errors['wpcf-images'] = 'Image file type is not supported: ' . $type;
                return array( $fields, $errors );
            }
        }
 
        // 4. Maximum file size should be 1MB.
        foreach ( $_FILES['wpcf-images']['size'] as $size ) {
            if ( $size >= $max_size ) {
                $errors['wpcf-images'] = 'An Image is too big. Images should less than 1MB';
                return array( $fields, $errors );
            }
        }
    }
 
    //return result
    return array($fields,$errors);
}

I'm trying to limit the number of images to 6 max, limit the file types to JPG, PNG, JPEG – and then limit the max size to 1MB.

Another question I have is does this still allow uploads great that the limit but just limits submitting the form or does is not allow the user to upload the image if it the wrong format or if the images is too big.

Something else I wonder about is the possibility to no use the built in WordPress media editor? Is that possible?

Thanks

#2113209

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Sorry, but I did not understand what would you mean in your last reply. So, I'll ask the following questions:
- You said the validation does not work, can you elaborate more? What is not working? Size validation? Type image validation? Can you provide a test scenario?

You asked about uploads, let me explain that PHP allows you to upload anything into a temporary folder. If the validation completes successfully, WordPress/Toolset will move the uploaded file from the temporary folder to its target destination. If the validation fails, the file will remain in the upload folder until the operating system removes it.

Your last question about the WordPress media editor is not clear to me. Can you elaborate more? Would you like to use it or not? Can you provide an example to help me understand?

#2113741

Hey Jamal. So I tried upload an image larger than 1MB (which should not have worked) but it allowed me to upload it. I was also able to upload more than 6 images (which again I shouldn't be able to do).

Thanks for explaining more about how PHP stores the files until the form is submitted and then verfies.

My final question is simply is there a way to have simple field that would upload a file without the WordPress media interface?

#2114333

Minesh
Supporter

Languages: English (English )

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

Jamal is not available at the moment and let me step in here. I hope this is OK.

Can you please share problem URL where you added the form as well as admin access details and also tell me where exactly you added the validation hook.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2115611

I figured this out on my own. I think the best solution for anyone using the WordPress media library is to limit is based on user role with WordPress function instead of Toolset.

Here is the code I used:

/**
 * Filter the upload size limit for non-administrators.
 *
 * @param string $size Upload size limit (in bytes).
 * @return int (maybe) Filtered size limit.
 */
function filter_site_upload_size_limit( $size ) {
    // Set the upload size limit to 1 MB for users lacking the 'manage_options' capability.
    if ( ! current_user_can( 'manage_options' ) ) {
        // 1 MB.
        $size = 1024 * 1000;
    }
    return $size;
}
add_filter( 'upload_size_limit', 'filter_site_upload_size_limit', 20 );

//For limiting file type
add_filter('upload_mimes','restict_mime'); 

function restict_mime($mimes) { 
    $mimes = array( 
                'jpg|jpeg|jpe' => 'image/jpeg', 
                'gif' => 'image/gif', 
                'png' => 'image/png', 
    ); 
    return $mimes;
}
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.