Skip Navigation

[Résolu] Restricting File Types on File Upload CRED Field

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Client is using the Forms cred_form_ajax_upload_validate hook to restrict the acceptable file types that can be uploaded for a file custom field, but it is not working, the accepted file types are being rejected.

Solution:
Their code wrongly defines the file types which do not match the file type reported by the global $_FILES object used for the test (e.g. they are testing for "application/text" instead of "text/plain".

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

This support ticket is created Il y a 5 années et 11 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par davidZ-4 Il y a 5 années et 11 mois.

Assisté par: Nigel.

Auteur
Publications
#1164528
fileupload error.PNG

I am trying to: Restricting File Types on File Upload CRED Field

Link to a page where the issue can be seen: lien caché

I expected to see: be able to upload files based on the code below

Instead, I got: error message from generated by the code

I used the code available at https://toolset.com/forums/topic/having-problem-with-restricting-file-types-on-file-upload-cred-field/ and modify it to the following:

//*Validate for csv xsl txt on File Upload
add_filter('cred_form_ajax_upload_validate','cred_filetype_validation',10,2);
function cred_filetype_validation($error_fields, $form_data)
{
    $file_types = array('application/csv','application/xls','application/txt');
      
      
    // Field data are field values and errors 
    list($fields,$errors)=$error_fields;
    
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
      
        if (in_array($form_data['id'], [386]) && (isset($_FILES['wpcf-u-sngl-portfolio-file']['type']))) {
        
            //Retrieve file type
            $file_type_uploaded=$_FILES['wpcf-u-sngl-portfolio-file']['type'];
               
            //Validate files uploaded, make sure its PDF file type
            if (!in_array($file_type_uploaded, $file_types) ) {
                   
                //Validation failed,
                // set error message per field
                //Remove wpcf - prefix here!
                $errors['File-Format-Error']='Sorry the file you have uploaded is not of the correct type. Accepted file types: csv, xls, txt';   
                       
            } 
        
        }
    //return result
    return array($fields,$errors);
}

i modified the code to except csv, xls, and txt file formats
when i try to upload a file with one of the exceptions csv, xls, or txt i still get the error that the file extension is not excepted.

please advise,
thanks,
David

#1164733

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Try dumping $_FILES to the debug.log to double-check what the correct type is.

When I just tried uploading a txt file, for example, it showed the type as "text/plain".

#1165024

My issue is resolved now. Thank you!