首页 > 解决方案 > 如何在 Wordpress 中上传 PowerPoint 幻灯片文件?

问题描述

我有一些带有 mime-type 的 PowerPoint 幻灯片文件 .ppsx,application/vnd.openxmlformats-officedocument.presentationml.slideshow我想上传到 WordPress。但是,当我尝试将其上传到媒体浏览器时,我收到错误“抱歉,出于安全原因,不允许使用此文件类型。”。

尽管 .ppsx 文件位于允许的文件类型和 mimetypes 列表中,但还是会这样做。

标签: wordpress

解决方案


当您上传文件时,WordPresswp-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 文件!


推荐阅读