wordpress - 如何在 Wordpress 中上传 PowerPoint 幻灯片文件?
问题描述
我有一些带有 mime-type 的 PowerPoint 幻灯片文件 .ppsx,application/vnd.openxmlformats-officedocument.presentationml.slideshow
我想上传到 WordPress。但是,当我尝试将其上传到媒体浏览器时,我收到错误“抱歉,出于安全原因,不允许使用此文件类型。”。
尽管 .ppsx 文件位于允许的文件类型和 mimetypes 列表中,但还是会这样做。
解决方案
当您上传文件时,WordPress会在wp-include/functions.php:2503
. 这些检查的一部分是使用 PHP 函数finfo_file()使用 PHP 检测到的 mimetype 验证文件的给定 mimetype 。
但是,finfo_file()
它并不总是准确的,其结果通常取决于操作系统。在 .ppsx 文件的特定情况下,finfo_file()
可以将 mimetype 读取为application/vnd.openxmlformats-officedocument.presentationml.presentation
. WordPress 将此视为潜在的安全风险,因为它与该文件扩展名的给定 mimetype 不匹配并关闭上传。
wp_check_filetype_and_ext()
还有一个过滤器,我们可以利用它来发挥我们的优势:
function my_check_filetype_and_ext( $info, $file, $filename, $mimes, $real_mime )
{
if ( empty( $check['ext'] ) && empty( $check['type'] ) )
{
$secondaryMimetypes = ['ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation'];
// Run another check, but only for our secondary mime and not on core mime types.
remove_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
$info = wp_check_filetype_and_ext( $file, $filename, $secondaryMimetypes );
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
}
return $info;
}
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
在 vanilla WordPress 中,没有办法为单个文件类型设置多个 mimetype。如果上述过滤器未能通过第一组对,则上述过滤器再次运行 mimetype 检查以查找第二组文件类型/mimetype 对。通过允许具有演示文稿 mimetype 的 .ppsx 文件,您现在可以上传 .ppsx 文件!
推荐阅读
- python - 如何在seaborn小提琴图中为每个组分配不同的位置
- c# - 在 WebApi 的查询中将日期时间转换为 Oracle 格式的问题 - “发生错误”消息
- html - How can I keep horizontal element alignment inside an expanding list item?
- python - nltk calc perplexity of bigram/trigram
- oracle - Oracle 从另一个表上随机更新
- python - 如何在熊猫中分组和聚合
- git - 如何在 TypeScript 网站上报告文档问题
- python - Panda 保留/添加在 groupby 和 agg 期间“丢失”的列
- reactjs - 对道具更改做出反应重新渲染
- python - Django:最早()和最新()需要字段作为位置参数或模型元数据中的“get_latest_by”