Skip Navigation

[Gelöst] Restricting File Types on File Upload CRED Field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 5 Jahren, 11 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von davidZ-4 vor 5 Jahren, 11 Monaten.

Assistiert von: Nigel.

Author
Artikel
#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: versteckter Link

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

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: 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!