Skip Navigation

[Resolved] File upload PDF only

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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by TomW5440 2 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2346291

Tell us what you are trying to do?
Trying to force PDF only on repeatable file upload for Ajax form. I found some code from Toolset docs but its not working. It is fairly old so it might be out dated now?

function my_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;
  
    //validate if specific form
    if ($form_data['id']==135)
    {
  
        //check if uploaded file is right type
        if ($fields['wp-cf-wills-and-expression-of-wish-forms']['field_data']['type'] != 'application/pdf')
        {
            //set error message for file
            $errors['wills-and-expression-of-wish-forms'] = 'Wrong file type';
        }
    }
  
    //return result
    return array($fields,$errors);
}
add_filter('cred_form_ajax_upload_validate','my_validation',10,2);

Is there any documentation that you are following?
https://toolset.com/forums/topic/restrict-user-to-upload-pdf-file-only/

#2346329

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Tom,

Thank you for getting in touch.

Can you replace "field_data" with"file_data".

It appears you're using the wrong attribute for the file array.

Please let me know if this helps.
Thanks,
Shane

#2346765

My issue is resolved now. The following code forces pdf only and limits upload size.

//Limit upload file size for repeating image custom field
add_filter('cred_form_validate','story_image_size_validation',10,2);
function story_image_size_validation($field_data, $form_data)
{
    list($fields,$errors)=$field_data;
    $arr = array(135);
    if (in_array($form_data[id], $arr)){
        if(isset($fields['wpcf-wills-and-expression-of-wish-forms']['file_data']['size']['type'])){
            if($fields['wpcf-wills-and-expression-of-wish-forms']['file_data']['size']>10000){
                $errors['wills-and-expression-of-wish-forms']='the image size should not be more than 10000 bytes';
            }
			if ($fields['wpcf-wills-and-expression-of-wish-forms']['file_data']['type'] != 'application/pdf'){ 
			 $errors['wills-and-expression-of-wish-forms']='only pdf format allowed';
			}
        }
    }
    return array($fields,$errors);