1

So, after the new WP updates my form will not allow certain files to upload. I have a Gravity form with an upload field to submit 3D print files. I can get one type of file to work, but not the others. Just hoping someone else has had this issue and has some update on a solution.

I'm using:

add_filter('upload_mimes','add_custom_mime_types',1,1);
function add_custom_mime_types($mime_types){
    $mime_types['stl'] =  'application/wavefront-stl';
    $mime_types['igs'] =  'application/iges';
    $mime_types['stp'] =  'application/step';
    $mime_types['step'] =  'application/step';

    return $mime_types;
}

Oddly, the .stl will upload but not the others. I have also installed Extra File Types plugin to add file types but no help there. Disable Real MIME Check did not work either.

I also added:

define( 'ALLOW_UNFILTERED_UPLOADS', true );

to wp-config and I can upload these files to the media library as an admin, but I need them to upload from my front-end form.

3
  • Have you included the media library scripts on the front-end wp_enqueue_media(); and also initialized the js uploaded`. Note that if you want your users to be able to upload, then you still need to give them the 'upload_files' capability as well. Commented Jan 14, 2019 at 23:55
  • furthermore, if the upload is still not working on the front-end, try inspecting the wp_handle_upload_prefilter filter and see the response you get. Commented Jan 14, 2019 at 23:58
  • I'm using Gravity Forms plugin to handle the file uploads. Is this something that should already be present in their code? Not quite sure how to see the response from the wp_handle_upload_prefilter.
    – shacker
    Commented Jan 15, 2019 at 20:21

1 Answer 1

1

Thanks to this thread here, I found this code

    function fix_wp_csv_mime_bug( $data, $file, $filename, $mimes ) {
    $wp_filetype = wp_check_filetype( $filename, $mimes );
            $ext = $wp_filetype['ext'];
            $type = $wp_filetype['type'];
            $proper_filename = $data['proper_filename'];

            return compact( 'ext', 'type', 'proper_filename' );
}
add_filter( 'wp_check_filetype_and_ext', 'fix_wp_csv_mime_bug', 10, 4 );

which does allow the files to be uploaded through the form. Not sure what I am opening myself up to, but it works for now.

1
  • It looks like you're only trusting the file extension, regardless of the file content, thus overriding all existing real mime type validations on all files. The latest security patch (5.0.1) update was to avoid trusting only the file extension. Here's another approach.
    – birgire
    Commented Jan 16, 2019 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.