猿问

如果在联系表格 7 中上传的文件是二进制文件,则阻止

有人可以帮助阻止联系表格7中的二进制文件,即如果game.exe重命名为game.docx并上传,那么它必须被阻止


到目前为止,我已经尝试在 wp-includes/functions.php 中添加以下代码


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 === 'your-file') {

        $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;

}

这允许我上传 pdf 文件并检查 pdf 是否为二进制和块。但在 rtf、doc 和 docx 中它不起作用


Helenr
浏览 99回答 1
1回答

慕田峪7331174

这是因为您正在检查错误的 mime 类型。.doc: application/msword .docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
随时随地看视频慕课网APP
我要回答