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

时间:2018-03-02 09:17:07

标签: php wordpress upload

Wordpress版本:4.7.9。

define('ALLOW_UNFILTERED_UPLOADS', true);

该陈述写入wp-includes/functions.php 编辑一个名为test的简单文件,其中不包含任何扩展名。

vim test
to upload the file which contain no file extension.

在上传文件test时收到错误。

This file type is not allowed. Please try another.

将文件test重命名为test.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;
     }

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

define('ALLOW_UNFILTERED_UPLOADS', true);

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

这是我的/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');
?>

它不会产生任何影响。

5 个答案:

答案 0 :(得分: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};

在问题解决之前对问题进行临时修复将挂钩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;
});

这样您就可以通过上传器上传test了。由于WordPress已在后端进行检查,只要您在wp-config.php ALLOW_UNFILTERED_UPLOADStrue的{​​{1}}中对其进行了定义,就应该正确上传。

答案 1 :(得分:3)

确保没有覆盖define('ALLOW_UNFILTERED_UPLOADS', true);的安全插件或其他插件:)

我建议您首先通过您的网站文件扫描ALLOW_UNFILTERED_UPLOADS,然后查看弹出的内容。

如果您还没有更新到最新版本的WordPress,因为您收到的错误消息与我得到的错误消息不同。

答案 2 :(得分:2)

您不希望将其粘贴到/functions.php文件中,而是粘贴到/wp-config.php文件中。

或者,您可以通过将其粘贴到/functions.php文件中来创建特定文件类型的MIME,就像您所做的那样。

答案 3 :(得分:2)

Chin Leung回答相当不错,唯一需要注意的是可以上传任何文件扩展名。

下面的方法需要#include <stdlib.h> #include <stdio.h> #include <string.h> #include "unit_tests.h" #ifdef __FreeBSD__ extern char *malloc_options ; #endif /* __FreeBSD__ */ // Run the unit tests or as a "normal program". // You can run this as a "normal program" if you want for a simple test of the $ int main( int argc, char *argv[] ) { #ifdef __FreeBSD__ malloc_options = "CFGSU" ; #endif /* __FreeBSD__ */ test() ; return 0 ; } 设置为false(或者根本不设置在wp-config或functions.php上),它允许不带扩展名的文件和不允许的块扩展名:

ALLOW_UNFILTERED_UPLOADS

答案 4 :(得分:1)

wp-config.php文件的头部添加以下代码:

define('ALLOW_UNFILTERED_UPLOADS', true);

注意:要在媒体上传新文件,请转到以下网址:

http://example.com/wp-admin/media-new.php

而不是:

http://example.com/wp-admin/upload.php

因为在upload.php中,mime类型验证是通过Javascript完成的。​​