Skip Navigation

[Resolved] How to set Upload file size and file type limit

This support ticket is created 3 years, 4 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by yosukeS 3 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1860949

Tell us what you are trying to do?

I would like to set my site's upload file type and data size limit. I would like to know for the whole site or individual forms( i have like 8 different forms)

I would like to set each file for max 5Mb and accept only jpg,png,gif,pdf,doc. and if the user try to
upload something other than this scope, I would like to
display an error message.

I think this is very important and basic feature for wp toolset form. But, why isn't it a basic feature??
I really don't get it though.

I try to use some code snippets from other posts. so far unsuccessful.

Is there any documentation that you are following?

https://toolset.com/forums/topic/how-to-limit-the-file-upload-size-limit-from-the-cred-form/

Is there a similar example that we can see?

What is the link to your site?

#1861259

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To apply validation on the uploaded file's type and size, you can use the "cred_form_validate" hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

You'll find a good example of this hook's usage in this reply:
https://toolset.com/forums/topic/restricting-file-size-and-types-on-file-upload-cred-form/#post-1411409

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1866097

Hello Waqar,

Thank you for information.

I copy the code from the link you gave me and modify it as below.

If I understand this correctly, it should limit for form id 1311 upload only PDF upto 5mg for a field "financial-info", right?
I use codesnipet plugin and added there. But it seems not working. I was able to upload 6mb pdf, and some jpeg as well.
Could you explain this little more?

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 (( 1311 == $form_data['id']) && (isset($_FILES['wpcf-financial-info']['type'])) && (isset($_FILES['wpcf-financial-info']['size']))) {

//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-financial-info']['type'];

//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-financial-info']['size'];

//Validate files uploaded, make sure its PDF file type AND not more than 5000kB
if (!( ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 5000000) )) {

$errors['pers-financial-info']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.';

}

}
//return result
return array($fields,$errors);
}

#1868561

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

I've tested the code that you've shared and it worked as expected on my test website. I just had to fix this line:


$errors['pers-financial-info']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.'; 

To:


$errors['financial-info']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.'; 

Also, this file validation approach will only work if the option "Use the WordPress Media Library manager for image, video, audio, or file fields" is not checked in the form's settings.

#1938365

Is there anyway to change what's written on my original post? I would like to hide my site name from this post. Could you delete it? also, Please delete this reply aswell after deleting my site name if possible?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.