Symfony:如何在ppt和pptx上获得正确的mime类型

Symfony:如何在ppt和pptx上获得正确的mime类型,symfony,mime-types,Symfony,Mime Types,我想将PowerPoint文件导入我的Symfony应用程序。 我允许这些mime类型进入白名单参数 co_upload_white_list: - "image/*" - "application/pdf" - "application/x-pdf" - "application/msword" - "application/vnd.openxmlformats-officedocument.presentationml.presentation"

我想将PowerPoint文件导入我的Symfony应用程序。 我允许这些mime类型进入白名单参数

co_upload_white_list: 
    - "image/*"
    - "application/pdf"
    - "application/x-pdf"
    - "application/msword"
    - "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    - "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    - "application/vnd.oasis.opendocument.text"
    - "application/vnd.ms-excel"
    - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    - "application/vnd.oasis.opendocument.spreadsheet"
    - "application/vnd.ms-powerpoint"
这段代码工作正常,但是Symfony/Component/HttpFoundation/File/mimetype/filebinarymetypeguesser.php在$File->getMimeType():application/octet stream返回的mimetype应该是application/vnd.openxmlformats-officedocument.presentationml.presentation

use Oneup\UploaderBundle\Uploader\Naming\NamerInterface;

class UploadListener implements NamerInterface
{
[....]

    public function onValidate(ValidationEvent $event)
    {

     /**
     * $file is oneup/uploader-bundle/Uploader/File/FilesystemFile.php
     */
    $file = $event->getFile();
    $mimeType = $file->getMimeType();
    $fileSize = $file->getSize();
    $ext = $file->getExtension();

    $hotel = $event->getRequest()->attributes->get('hotel');
    $locale = ($hotel->getUserLocale()) ? $hotel->getUserLocale() : 'en';

    if (explode("/", $mimeType)[0] != "image" && !in_array($mimeType, $this->uploadWhiteList)) {

        $this->messenger->error("MimeType invalid: %mimeType%", ['%mimeType%' => $mimeType], $locale);
        throw new HttpException(400, "File type invalid, mimetype is '$mimeType', allowed=".json_encode($this->uploadWhiteList));
    }
[...]
}
有没有办法获得正确的mime类型?

尝试使用

输出:

应用程序/vnd.ms-powerpoint

使用symfony这对我很有用(symfony 3.4)

相同输出:

应用程序/vnd.ms-powerpoint


谢谢我将完成我的代码,从ValidationEvent返回$event。我的类是事件侦听-{name:“kernel.event\u listener”,event:“oneup\u uploader.validation”,method:“onValidate”}Add:$file是一个oneup/uploader包/uploader/file/FilesystemFile.php,它扩展UploadedFile并实现FileInterface。和getMimeType()返回应用程序/octetStream
echo mime_content_type("example.ppt");
    $filepath = "/tmp/example.ppt";
    file_put_contents($filepath, file_get_contents("https://sample-videos.com/ppt/Sample-PPT-File-500kb.ppt"));
    $file = new \Symfony\Component\HttpFoundation\File\File($filepath);
    echo $file->getMimeType();