• Resolved navasahmed

    (@navasahmed)


    Hello,

    How can I declare acceptable file types using MIME types.is there any hook have? I have tried with contact form 7 hooks but it’s not worked. can you please check it we need to solve this issue ASAP.

    example:

    add_filter('wpcf7_validate_file*', 'cf7_custom_file_validation', 10, 2);
    add_filter('wpcf7_validate_file', 'cf7_custom_file_validation', 10, 2);
    
    function cf7_custom_file_validation ($result, $tag) {
        if ($tag->name === 'file-123') {
            $contentType = mime_content_type($_FILES[$tag->name]['tmp_name']);
    
            if ($contentType !== 'application/docx' && $contentType !== 'application/pdf' && $contentType !== 'application/doc'&& $contentType !== 'application/rtf') {
                $result->invalidate($tag, 'This file type is not supported');
            }
        }
    
        return $result;
    }

    Thanks

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Glen Don Mongaya

    (@glenwpcoder)

    Hello @navasahmed ,

    You could try this filter.

    add_filter( 'wpcf7_validate_mfile',  'cf7_custom_file_validation', 10, 2 );
    add_filter( 'wpcf7_validate_mfile*',  'cf7_custom_file_validation', 10, 2 );
    
    function cf7_custom_file_validation ($result, $tag) {
        if ($tag->name === 'file-123') {
            $files =  $_POST[ $tag->name ][0]; // this could be an array
            $type = wp_check_filetype( $files );
            $contentType = $type['type'];
            if ($contentType !== 'application/docx' && $contentType !== 'application/pdf' && $contentType !== 'application/doc'&& $contentType !== 'application/rtf') {
                $result->invalidate($tag, 'This file type is not supported');
            }
        }
    
        return $result;
    }
    Thread Starter navasahmed

    (@navasahmed)

    Hello @glenwpcoder,

    Thanks for your reply.

    I have tested you shared code. unfortunately, it’s not working. the error message not showing(screenshot: https://snipboard.io/JyRf9K.jpg ). here am attaching the field that I’m using on my contact form 7. please try it from your end. we need to solve the issue ASAP.

    [mfile download filetypes:xlsx|xls|pdf|doc|docx class:file-upload limit:5000000]

    validation code:

    add_filter( 'wpcf7_validate_mfile',  'cf7_custom_file_validation', 10, 2 );
    add_filter( 'wpcf7_validate_mfile*',  'cf7_custom_file_validation', 10, 2 );
    
    function cf7_custom_file_validation ($result, $tag) {
        if ($tag->name === 'download') {
            $files =  $_POST[ $tag->name ][0]; // this could be an array
            $type = wp_check_filetype( $files );
            $contentType = $type['type'];
            if ($contentType !== 'application/docx' && $contentType !== 'application/pdf' && $contentType !== 'application/doc'&& $contentType !== 'application/xlsx'&& $contentType !== 'application/xls') {
                $result->invalidate($tag, 'This file type is not supported..........');
            }
        }
    
        return $result;
    }

    Thanks.

    • This reply was modified 2 years, 7 months ago by navasahmed.
    • This reply was modified 2 years, 7 months ago by navasahmed.
    Thread Starter navasahmed

    (@navasahmed)

    Hello @glenwpcoder,

    Any Updates on my query? please check it we need to solve the issue ASAP.

    Thanks.

    Plugin Author Glen Don Mongaya

    (@glenwpcoder)

    Hello @navasahmed ,

    Can you print_r the $files & $type and send me the screenshot of the result?

    Thanks.

    Thread Starter navasahmed

    (@navasahmed)

    Hello @glenwpcoder,

    Another issue I am facing is that the mandatory filed error does not appear after the drag and drop field https://snipboard.io/LtpY2j.jpg.

    Also, I’ve tried printing $files & $type but the form keeps loading.

    Thanks

    Plugin Author Glen Don Mongaya

    (@glenwpcoder)

    Maybe the print_r caused the error not to show the validation message.

    Just remove the print_r.

    Also, I tried your code in my local and it’s working correctly see here – https://snipboard.io/NozK3x.jpg

    Plugin Author Glen Don Mongaya

    (@glenwpcoder)

    Can you contact me through my email [email protected] for further testing and I would like to request for temporary admin access if possible.

    Thread Starter navasahmed

    (@navasahmed)

    Hello @glenwpcoder,

    Did you try adding a drag-and-drop field at the end of the form? If so, please add the field between any other fields, like https://snipboard.io/BjAOMQ.jpg. Afterwards, add this code and check the validation for the Drag and Drop field and also for the fields after that.

    Thanks

    Thread Starter navasahmed

    (@navasahmed)

    Hello,

    Any update on the above query?

    Thanks

    Plugin Author Glen Don Mongaya

    (@glenwpcoder)

    Hey @navasahmed , will take a look again today.

    Thread Starter navasahmed

    (@navasahmed)

    Hello,

    Any updates on the above query? the issue is still on the site. we can upload renamed files. here I will breaf down my question.

    if game.exe is renamed as game.pdf and uploaded then it needs to be blocked. currently, it’s not blocking.

    I have checked your shared code and it’s only checking the wp_check_filetype() I think it only gives the file extension, not checking the file mime type. can you please check it the issue.

    also please check your mail. i have contacted through there also.

    Thanks

    Plugin Author Glen Don Mongaya

    (@glenwpcoder)

    Hello @navasahmed ,

    Thanks, I received your email.

    I’m currently updating so I can release a new version that includes mime type validation.

    My apology for the delay, expect an update within this week.

    Thanks,
    Glen

    Thread Starter navasahmed

    (@navasahmed)

    from your end also that issue was reproduced right?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘MIME types file validation’ is closed to new replies.