Skip Navigation

[Resolved] restricte file upload type

This support ticket is created 2 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by salimA 2 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#2173047

Dears,

I am trying to restrict the upload file type to be only ( PDF, JPG ,PNG,JPEG), I am using the below code, but it prevents to upload any type except .pdf . What is the wrong

-------------------------------------------------------------------------------------------------------
<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

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 (( 5245 == $form_data['id']) && (isset($_FILES['wpcf-card']['type'])) && (isset($_FILES['wpcf-card']['size']))) {
if ( (5245 == $form_data['id']) && (!empty($_POST['wpcf-card'])) ) {

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

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

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

$errors['card']='Sorry the file you have uploaded is not of the correct type or exceeded 2M limit.';

}

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

#2173125

Jamal
Supporter

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

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

Hello and thank you for contacting Toolset support.

I don't think that the following mime types are correct:
- application/jpg
- application/png
- application/jpeg
You should probably use (image/jpg, image/png, and image/jpeg) instead.

What I suggest, is to use the error_log function to write the mime types into the logs, and perform a couple of uploads of the different files to see what are the correct mime types. Then update the code accordingly.
hidden link

Check an example here https://toolset.com/forums/topic/having-problem-with-restricting-file-types-on-file-upload-cred-field/

#2174701

My issue is resolved now. Thank you!

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