【发布时间】:2014-02-25 17:22:24
【问题描述】:
我想让网站访问者能够向我发送不同的、非常规的文件类型。就像 PCB 文档或 .psd 文件一样。 我查看了所有代码,没有找到任何提示。 我想要的只是禁用文件类型检查。 另外,我在某处读到它与 Wordpress 配置文件有关,没关系,我会更改它们。 提前谢谢你。
【问题讨论】:
标签: wordpress forms contact file-type
我想让网站访问者能够向我发送不同的、非常规的文件类型。就像 PCB 文档或 .psd 文件一样。 我查看了所有代码,没有找到任何提示。 我想要的只是禁用文件类型检查。 另外,我在某处读到它与 Wordpress 配置文件有关,没关系,我会更改它们。 提前谢谢你。
【问题讨论】:
标签: wordpress forms contact file-type
它在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 字节)。
【讨论】:
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';
你很高兴……
【讨论】: