使用wordpress媒体上传PDF或DOC文件与订阅者给我一个错误

时间:2014-04-02 06:47:36

标签: php wordpress wordpress-plugin buddypress

我使用过BuddyPress Docs。当我尝试从我的前端网站上传图像时。它将与订户成功上传。但是当我尝试上传.xlsx文件的.PDF时,它会给我一个错误

  

抱歉,出于安全原因,不允许使用此文件类型。

但是当我将用户的角色更改为管理员而不是.PDF时,.xlsx文件也会被上传。 那么如何将所有文件类型的媒体权限授予所有用户角色。 我还在我的/wp-content/themes/my-theme/functions.php

中添加了以下内容
add_filter('upload_mimes','add_custom_mime_types');
function add_custom_mime_types($mimes) {
    return array_merge($mimes,array (
            'pdf'                          => 'application/pdf',
            'doc'                          => 'application/msword',
            'pot|pps|ppt'                  => 'application/vnd.ms-powerpoint',
            'wri'                          => 'application/vnd.ms-write',
            'xla|xls|xlt|xlw'              => 'application/vnd.ms-excel',
            'mdb'                          => 'application/vnd.ms-access',
            'mpp'                          => 'application/vnd.ms-project',
            'docx'                         => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'docm'                         => 'application/vnd.ms-word.document.macroEnabled.12',
            'dotx'                         => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
            'dotm'                         => 'application/vnd.ms-word.template.macroEnabled.12',
            'xlsx'                         => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'xlsm'                         => 'application/vnd.ms-excel.sheet.macroEnabled.12',
            'xlsb'                         => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
            'xltx'                         => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
            'xltm'                         => 'application/vnd.ms-excel.template.macroEnabled.12',
            'xlam'                         => 'application/vnd.ms-excel.addin.macroEnabled.12',
            'pptx'                         => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
            'pptm'                         => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
            'ppsx'                         => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
            'ppsm'                         => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
            'potx'                         => 'application/vnd.openxmlformats-officedocument.presentationml.template',
            'potm'                         => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
            'ppam'                         => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
            'sldx'                         => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
            'sldm'                         => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
            'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
    ));
}

2 个答案:

答案 0 :(得分:0)

WordPress不允许我们在媒体库中上传所有类型的文件,即使您收到错误的zip文件也是如此。

方法1:

过滤钩

正确的功能如下(将此代码放在function.php文件中),如果您想允许其他文件类型,只需将它们添加到$exisiting_mimes数组中:

<?php
add_filter('upload_mimes', 'add_custom_upload_mimes');
if (!function_exists('add_custom_upload_mimes')) {
 function add_custom_upload_mimes( $existing_mimes ){
  $existing_mimes['zip']  = 'application/zip';
  $existing_mimes['swf']  = 'application/x-shockwave-flash';
  $existing_mimes['rtf']  = 'text/richtext';
  $existing_mimes['tiff'] = 'image/tiff';
  $existing_mimes['pdf']  = 'application/pdf';
  $existing_mimes['doc']  = 'application/msword';
  return $existing_mimes;
 }
}
?>

还有其他方法可以禁用文件类型上传限制。对于某些人来说,第一种方法是可以的,但对于其他人,他们不断在博客上上传不同类型的文件,这不是一个好的解决方案。

方法2:

禁用过滤

在WordPress中禁止某些类型文件的过程称为过滤。您可以使用以下代码禁用此过滤。您必须在wp-config.php中添加它。 define('ALLOW_UNFILTERED_UPLOADS', true);

注意:现在您已允许上传博客上的所有类型的文件。这可能会导致严重的安全问题。如果管理不当,请注意上传到服务器上的任何文件都可以执行。

答案 1 :(得分:0)

Just Replace with this code

More Info

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
    $existing_mimes['pdf']                          = 'application/pdf';
    $existing_mimes['doc']                          = 'application/msword';
    $existing_mimes['pot|pps|ppt']                  = 'application/vnd.ms-powerpoint';
    $existing_mimes['wri']                          = 'application/vnd.ms-write';
    $existing_mimes['xla|xls|xlt|xlw']              = 'application/vnd.ms-excel';
    $existing_mimes['mdb']                          = 'application/vnd.ms-access';
    $existing_mimes['mpp']                          = 'application/vnd.ms-project';
    $existing_mimes['docx']                         = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
    $existing_mimes['docm']                         = 'application/vnd.ms-word.document.macroEnabled.12';
    $existing_mimes['dotx']                         = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
    $existing_mimes['dotm']                         = 'application/vnd.ms-word.template.macroEnabled.12';
    $existing_mimes['xlsx']                         = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
    $existing_mimes['xlsm']                         = 'application/vnd.ms-excel.sheet.macroEnabled.12';
    $existing_mimes['xlsb']                         = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
    $existing_mimes['xltx']                         = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
    $existing_mimes['xltm']                         = 'application/vnd.ms-excel.template.macroEnabled.12';
    $existing_mimes['xlam']                         = 'application/vnd.ms-excel.addin.macroEnabled.12';
    $existing_mimes['pptx']                         = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
    $existing_mimes['pptm']                         = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
    $existing_mimes['ppsx']                         = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
    $existing_mimes['ppsm']                         = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
    $existing_mimes['potx']                         = 'application/vnd.openxmlformats-officedocument.presentationml.template';
    $existing_mimes['potm']                         = 'application/vnd.ms-powerpoint.template.macroEnabled.12';
    $existing_mimes['ppam']                         = 'application/vnd.ms-powerpoint.addin.macroEnabled.12';
    $existing_mimes['sldx']                         = 'application/vnd.openxmlformats-officedocument.presentationml.slide';
    $existing_mimes['sldm']                         = 'application/vnd.ms-powerpoint.slide.macroEnabled.12';
    $existing_mimes['onetoc|onetoc2|onetmp|onepkg'] = 'application/onenote';
    return $existing_mimes;
}