php - Wordpress:允许上传媒体中的 zip 文件
问题描述
我需要将 .zip 文件上传到媒体。
在这里我试过
add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
// add your extension to the mimes array as below
$existing_mimes['zip'] = 'application/zip';
$existing_mimes['gz'] = 'application/x-gzip';
return $existing_mimes;
}
但它不适用于 Wordpress 5+ 或古腾堡编辑器
解决方案
由于 WordPress 版本 4.7.1 或更高版本,它为 mime 类型添加了一些额外的安全检查。只需在活动主题的 functions.php 中添加以下代码片段即可实现上述目的 -
function modify_upload_mimes ( $mimes_types ) {
// add your extension to the mimes array as below
$mimes_types['zip'] = 'application/zip';
$mimes_types['gz'] = 'application/x-gzip';
return $mimes_types;
}
add_filter( 'upload_mimes', 'modify_upload_mimes', 99 );
function add_allow_upload_extension_exception( $types, $file, $filename, $mimes ) {
// Do basic extension validation and MIME mapping
$wp_filetype = wp_check_filetype( $filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
if( in_array( $ext, array( 'zip', 'gz' ) ) ) { // it allows zip files
$types['ext'] = $ext;
$types['type'] = $type;
}
return $types;
}
add_filter( 'wp_check_filetype_and_ext', 'add_allow_upload_extension_exception', 99, 4 );
欲了解更多信息,请访问我的博客链接
推荐阅读
- javascript - 如何将本地存储添加到我的待办事项 vue.js?
- r - R散点图警告消息“不是图形参数”
- c# - 通过asp net mvc控制器将文件application.pdf返回到浏览器显示代码而不是文件
- java - Java CountDownLatch 不解锁?为什么最后一行不打印?
- java - 如何在不知道其形式参数类型的情况下找到构造函数?
- php - 从加速度计算 g 力 1 秒间隔
- android-studio - 尝试一次将用户信息输入到 2 个表 sql
- android - Android中可调整的“分享方式”
- laravel - Laravel 默认语言环境不在 url
- javascript - 如何在页面加载时自动聚焦文本表单字段但在移动设备上隐藏软键盘?