【问题标题】:Wordpress Contact Form 7 check file server sideWordpress Contact Form 7 检查文件服务器端
【发布时间】:2018-07-13 15:38:20
【问题描述】:

这是我的问题:

在 Contact Form 7 制作的表格中,我允许下载 pdf、jpg 或 jpeg 文件类型。 我担心的是扩展控制没有完全执行。

确实,如果我将“test.exe”文件重命名为“text.jpg”,我可以提交我的表单并且没有错误...

有解决方案来测试这个吗? (例如通过 mime 测试)

如果是这样,我应该在哪里实现代码?

提前感谢您的回答,对不起我的英语(我是法国人......)

【问题讨论】:

    标签: wordpress file attachment contact-form-7


    【解决方案1】:

    我在我们的论坛中为您找到了一些有用的答案: PHP Uploading files - image only checking

    试试这个答案:

    <?php
      function isImage($img){
          return (bool)getimagesize($img);
      }
    ?>
    

    由杰里米·哈里斯发布。

    这里是 getimagesize() 的手册 http://php.net/manual/pl/function.getimagesize.php

    【讨论】:

    • 感谢您的回答。在完整的 php 中,我没有问题。我的问题是将它集成到 Wordpress 中(我是 wordpress 新手),我不想破坏 Contact From 插件(以便以后更新)。
    【解决方案2】:

    Wordpress 使用过滤器upload_mimes 来控制站点范围内允许哪些 MIME 类型。您可以通过将以下内容添加到 wp-includes/functions.php 来自定义此列表:

    function safe_mime_types($mime_types){
        unset($mime_types['exe']);  //remove .exe support
        return $mime_types;
    }
    add_filter('upload_mimes', 'safe_mime_types', 1, 1);
    

    您还可以添加可接受的 MIME 类型:

    function safe_mime_types($mime_types){
        $mime_types['svg'] = 'image/svg+xml';  //add .svg support
        unset($mime_types['exe']);
        return $mime_types;
    }
    add_filter('upload_mimes', 'safe_mime_types', 1, 1);
    

    【讨论】:

    • 谢谢,但我不想在整个网站上这样做。仅在特定页面中(使用联系表 7 制作)。
    【解决方案3】:

    对于那些有同样问题的人,这里是我的解决方案:

    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 === 'file-586') {
            $contentType = mime_content_type($_FILES[$tag->name]['tmp_name']);
    
            if ($contentType !== 'image/png' && $contentType !== 'image/jpeg' && $contentType !== 'application/pdf') {
                $result->invalidate($tag, 'Ce type de fichier n\'est pas supporté');
            }
        }
    
        return $result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 2011-03-13
      • 2016-11-24
      • 1970-01-01
      • 2014-11-23
      相关资源
      最近更新 更多