【问题标题】:Contact Form 7 - File Types [Wordpress]联系表 7 - 文件类型 [Wordpress]
【发布时间】:2014-02-25 17:22:24
【问题描述】:

我想让网站访问者能够向我发送不同的、非常规的文件类型。就像 PCB 文档或 .psd 文件一样。 我查看了所有代码,没有找到任何提示。 我想要的只是禁用文件类型检查。 另外,我在某处读到它与 Wordpress 配置文件有关,没关系,我会更改它们。 提前谢谢你。

【问题讨论】:

    标签: wordpress forms contact file-type


    【解决方案1】:

    它在the contact form 7 website上列出。

    可以上传的文件类型有:

    当您未明确设置 filetypes: 和 limit:(文件大小)选项时,Contact Form 7 对文件类型和文件大小应用默认限制。默认可接受的文件类型(扩展名)为:jpg、jpeg、png、gif、pdf、doc、docx、ppt、pptx、odt、avi、ogg、m4a、mov、mp3、mp4、mpg、wav 和 wmv。默认可接受的文件大小为 1 MB(1048576 字节)。

    【讨论】:

      【解决方案2】:

      include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
      if (!is_plugin_active('contact-form-7/wp-contact-form-7.php')) {
        return;
      }
      
      require_once ABSPATH.'wp-content/plugins/contact-form-7/wp-contact-form-7.php';
      
      remove_filter( 'wpcf7_validate_file', 'wpcf7_file_validation_filter', 10, 2 );
      remove_filter( 'wpcf7_validate_file*', 'wpcf7_file_validation_filter', 10, 2 );
      
      add_filter( 'wpcf7_validate_file', 'wpcf7_custom_file_validation_filter', 10, 2 );
      add_filter( 'wpcf7_validate_file*', 'wpcf7_custom_file_validation_filter', 10, 2 );
      
      function wpcf7_custom_file_validation_filter($result, $tag){
        $tag = new WPCF7_Shortcode( $tag );
      
        $name = $tag->name;
        $id = $tag->get_id_option();
      
        $file = isset( $_FILES[$name] ) ? $_FILES[$name] : null;
      
        if ( $file['error'] && UPLOAD_ERR_NO_FILE != $file['error'] ) {
          $result->invalidate( $tag, wpcf7_get_message( 'upload_failed_php_error' ) );
          return $result;
        }
      
        if ( empty( $file['tmp_name'] ) && $tag->is_required() ) {
          $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
          return $result;
        }
      
        if ( ! is_uploaded_file( $file['tmp_name'] ) )
          return $result;
      
        $allowed_file_types = array();
      
        if ( $file_types_a = $tag->get_option( 'filetypes' ) ) {
          foreach ( $file_types_a as $file_types ) {
            $file_types = explode( '|', $file_types );
      
            foreach ( $file_types as $file_type ) {
              $file_type = trim( $file_type, '.' );
              $file_type = str_replace( array( '.', '+', '*', '?' ),
                array( '\.', '\+', '\*', '\?' ), $file_type );
              $allowed_file_types[] = $file_type;
            }
          }
        }
      
        $allowed_file_types = array_unique( $allowed_file_types );
        $file_type_pattern = implode( '|', $allowed_file_types );
      
        $allowed_size = 1048576; // default size 1 MB
      
        if ( $file_size_a = $tag->get_option( 'limit' ) ) {
          $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/';
      
          foreach ( $file_size_a as $file_size ) {
            if ( preg_match( $limit_pattern, $file_size, $matches ) ) {
              $allowed_size = (int) $matches[1];
      
              if ( ! empty( $matches[2] ) ) {
                $kbmb = strtolower( $matches[2] );
      
                if ( 'kb' == $kbmb )
                  $allowed_size *= 1024;
                elseif ( 'mb' == $kbmb )
                  $allowed_size *= 1024 * 1024;
              }
      
              break;
            }
          }
        }
      
        /* File type validation */
      
        // Default file-type restriction
        if ( '' == $file_type_pattern )
          $file_type_pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv|ai|eps|tif';
      
        $file_type_pattern = trim( $file_type_pattern, '|' );
        $file_type_pattern = '(' . $file_type_pattern . ')';
        $file_type_pattern = '/\.' . $file_type_pattern . '$/i';
      
        if ( ! preg_match( $file_type_pattern, $file['name'] ) ) {
          $result->invalidate( $tag, wpcf7_get_message( 'upload_file_type_invalid' ) );
          return $result;
        }
      
        /* File size validation */
      
        if ( $file['size'] > $allowed_size ) {
          $result->invalidate( $tag, wpcf7_get_message( 'upload_file_too_large' ) );
          return $result;
        }
      
        wpcf7_init_uploads(); // Confirm upload dir
        $uploads_dir = wpcf7_upload_tmp_dir();
        $uploads_dir = wpcf7_maybe_add_random_dir( $uploads_dir );
      
        $filename = $file['name'];
        $filename = wpcf7_canonicalize( $filename );
        $filename = sanitize_file_name( $filename );
        $filename = wpcf7_antiscript_file_name( $filename );
        $filename = wp_unique_filename( $uploads_dir, $filename );
      
        $new_file = trailingslashit( $uploads_dir ) . $filename;
      
        if ( false === @move_uploaded_file( $file['tmp_name'], $new_file ) ) {
          $result->invalidate( $tag, wpcf7_get_message( 'upload_failed' ) );
          return $result;
        }
      
        // Make sure the uploaded file is only readable for the owner process
        @chmod( $new_file, 0400 );
      
        if ( $submission = WPCF7_Submission::get_instance() ) {
          $submission->add_uploaded_file( $name, $new_file );
        }
      
        return $result;
      }

      将此 sn-p 粘贴到您的 functions.php 文件中 在末尾附加您所需的文件类型 排队

      $file_type_pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv|ai|eps|tif';

      你很高兴……

      【讨论】:

      • 你能解释一下这段代码在做什么,或者你为什么建议这样做吗?仅代码的答案有时并没有真正的帮助。
      • 这是问题的答案,他要求扩展联系表格 7 以发送联系表格 7 本身不支持的文件扩展名。使用此代码,您可以扩展支持的文件扩展名。跨度>
      • 有趣的是......上面的代码几乎逐字出现在一个公开可用的 WordPress 插件中。可悲的是,这个答案或上述插件中都没有出现归属。 〜来吧人们;我们可以做得比这更好!
      • 实际上,根据他们的文档 (contactform7.com/file-uploading-and-attachment):“当您不设置文件类型时,联系表格 7 对文件类型 (...) 应用默认限制”。这意味着如果您设置文件类型,它将接受其他文件类型,而不是 wordpress 默认接受的文件类型。我现在进行了测试,它可以与 .zip 和 .rvt 一起使用,并且都可以使用。
      猜你喜欢
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 2020-05-30
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多