Skip Navigation

[Resolved] blocking .heic file extension

This support ticket is created 3 years, 7 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 5 replies, has 3 voices.

Last updated by Minesh 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2071531

Tell us what you are trying to do?
I'm trying to block users from uploading .heic files. They are not allowed and do not work in another form plugin but in post forms the file extension is able to be uploaded and then causes issues with the website.
Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#2071715

Hello and thank you for contacting Toolset support.

This will need custom code to validate which file types are allowed to be uploaded. Check an example that uses the cred_form_validate hook: https://toolset.com/forums/topic/limit-file-types-to-upload/#post-242237
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

You may also want to validate AJAX uploads, use the cred_form_ajax_upload_validate hook for that
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

I hope this helps. Let me know if you have any questions.

#2072797

I have not attempted to implement this yet. Looking at the code on the first link, maybe I'm wrong, but it looks like the first if to verify the form should have { } around the rest of the code?

I want all forms with upload options to only allow the allowed wordpress images. Why doesn't toolset abide by what is set in wordpress or if they are going to do this natively on their own, have a spot to set the allowed filetypes vs having to add to the functions.php. Seems it's a step backwards in a tool.

#2073697

You are right, the code has some errors. I run a test on a clean install and I was able to limit this file extension from uploading. You can login to my test site with the following URL hidden link
The custom code that I used is:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data){
  
  list($fields,$errors) = $error_fields;
  // validate if specific form
  if ( $form_data['id'] == 6 ) {
    $file = $fields['wpcf-my_field']['value'];
    $file_arr = explode('.', $file);
    if ( end($file_arr) == 'heic' ){
      // set error message per field
      $errors['my_field']='HEIC files are not allowed';
    }
  }
  
  // return result
  return array($fields,$errors);
}

You can test it here hidden link

Regarding your question Why doesn't toolset abide by what is set in wordpress
It turns out that WordPress does not limit this file extension. I was able to upload it from the WordPress library. I assume that it will need custom code to be restricted in the WordPress media library.

I hope this helps. Let me know if you have any questions.

#2073973

Ok. I understand that. I'll need to wrap my head around the changes I'll need for my purposes. I have 4 forms, 2 for adding and 2 for editing. One of the forms (add and edit) only has a single image so with just editing the if for the form to include both, this should work fine changing wpcf-my_field to wpcf-profile-picture and my_field to profile-picture. The other form, however, has 2 image fields. One is a normal image field "property-featured-image" but the other, "property-images" has "Allow multiple instances of this field " selected as the user can add as many images as they want. How would I handle the multiple instances? I think I can figure out the rest once I know how to refer to the multiple instances.

#2074343

Minesh
Supporter

Languages: English (English )

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

Jamal is on vacation. This is Minesh here and I'll take care of this ticket. Hope this is ok.

When you have repeating field, the values will be available as array.

For example - if yo upload two images:
$fields['image_field']['value'][0],
$fields['image_field']['value'][1]

So, you can navigate through all the file name values using for /foreach loop. For example:

foreach($fields['image_field']['value'] as $k=>$v):
 $file_arr = explode('.', $v);
    if ( end($file_arr) == 'heic' ){
    
      $errors['wpcf-image_field']='One of the uploaded file having HEIC extension. HEIC files are not allowed';
    }
endforeach;