将不包含扩展名的文件上传到wordpress?

it_*_*ure 9 php wordpress upload

Wordpress版本:4.7.9.

define('ALLOW_UNFILTERED_UPLOADS', true);
Run Code Online (Sandbox Code Playgroud)

声明写入wp-includes/functions.php.
编辑一个名为test不包含任何扩展名的简单文件.

vim test
to upload the file which contain no file extension.
Run Code Online (Sandbox Code Playgroud)

上传文件时出错test.

This file type is not allowed. Please try another.
Run Code Online (Sandbox Code Playgroud)

将文件重命名testtest.txt.
并添加以下内容wp-includes/functions.php.

add_filter('upload_mimes','custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
     $existing_mimes['txt'] = 'application/txt';
     return $existing_mimes;
     }
Run Code Online (Sandbox Code Playgroud)

该文件test.txt可以成功上传.
在配置文件中设置是没有用的wp-config.php.

define('ALLOW_UNFILTERED_UPLOADS', true);
Run Code Online (Sandbox Code Playgroud)

我正在使用二十四岁的孩子.

这是我的 /var/www/html//wp-content/themes/twentyfourteen-child/functions.php

<?php
define('ALLOW_UNFILTERED_UPLOADS', true);
function my_theme_enqueue_styles() {

$parent_style = 'twentyfourteen-style'; // This is 'twentyfourteen-style' for the Twenty Fourteen theme.


wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
?>
Run Code Online (Sandbox Code Playgroud)

它不会有任何影响.

Chi*_*ung 4

真正的问题不在于 WordPress 的后端,而在于前端验证。上传器由Plupload处理,默认情况下,WordPress 仅检查您是否ALLOW_UNFILTERED_UPLOADS在上传进度中定义,而没有真正使用该值调整前端插件的过滤器验证。也许是前端的一个小故障。

如您所见,WordPress 始终呈现以下默认设置:

var _wpPluploadSettings = {"defaults":{"file_data_name":"async-upload","url":"\/wp-admin\/async-upload.php","filters":{"max_file_size":"268435456b","mime_types":[{"extensions":"jpg,jpeg,jpe,gif,png,bmp,tiff,tif,ico,asf,asx,wmv,wmx,wm,avi,divx,flv,mov,qt,mpeg,mpg,mpe,mp4,m4v,ogv,webm,mkv,3gp,3gpp,3g2,3gp2,txt,asc,c,cc,h,srt,csv,tsv,ics,rtx,css,htm,html,vtt,dfxp,mp3,m4a,m4b,ra,ram,wav,ogg,oga,flac,mid,midi,wma,wax,mka,rtf,js,pdf,class,tar,zip,gz,gzip,rar,7z,psd,xcf,doc,pot,pps,ppt,wri,xla,xls,xlt,xlw,mdb,mpp,docx,docm,dotx,dotm,xlsx,xlsm,xlsb,xltx,xltm,xlam,pptx,pptm,ppsx,ppsm,potx,potm,ppam,sldx,sldm,onetoc,onetoc2,onetmp,onepkg,oxps,xps,odt,odp,ods,odg,odc,odb,odf,wp,wpd,key,numbers,pages"}]},"multipart_params":{"action":"upload-attachment","_wpnonce":"9ee7fbf228"}},"browser":{"mobile":false,"supported":true},"limitExceeded":false};
Run Code Online (Sandbox Code Playgroud)

在最终解决问题之前,对问题的临时解决方案是挂钩 WordPress 调用以生成前端设置的过滤器plupload_default_settings

将过滤器添加到您的functions.php

add_filter('plupload_default_settings', function ($settings) {
    if (defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS) {
        unset($settings['filters']['mime_types']);
    }

    return $settings;
});
Run Code Online (Sandbox Code Playgroud)

test这将允许您通过上传器上传您的内容。由于 WordPress 已经在后端进行检查,因此只要您在wp-config.phpthat ALLOW_UNFILTERED_UPLOADSis中定义了它true,它就应该正确上传。