gpt4 book ai didi

php - WordPress : Allow zip files in media to upload

转载 作者:行者123 更新时间:2023-12-05 08:25:59 28 4
gpt4 key购买 nike

我需要将 .zip 文件上传到媒体。

这里我试过了

https://wordpress.stackexchange.com/questions/57603/is-it-possible-to-allow-zip-files-to-be-uploaded-in-wordpress

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+ 或 gutenburg 编辑器

最佳答案

自 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 );

有关更多信息,请访问我的博客 link

关于php - WordPress : Allow zip files in media to upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59242333/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com