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

时间:2019-09-19 14:54:32

标签: symfony mime-types

我想将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 / FileBinaryMimeTypeGuesser.php在$ file-> getMimeType()处返回的模仿类型:application / octet-stream,但应为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));
    }
[...]
}

有没有办法获得正确的哑剧类型?

2 个答案:

答案 0 :(得分:0)

尝试使用mime_content_type

echo mime_content_type("example.ppt");

输出:

  

application / vnd.ms-powerpoint

Try this

使用symfony,这对我有效(symfony 3.4);

    $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();

相同的输出:

  

application / vnd.ms-powerpoint

答案 1 :(得分:0)

我用mimetype命令创建了一个新的猜测器。

<?php

namespace CoreBundle\Utils;

use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;

class MimeTypeGuesser implements  \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
{
private $cmd;

public function __construct($cmd = 'mimetype -i -b %s')
{
    $this->cmd = $cmd;
}

public static function isSupported()
{
    return \function_exists('mimetype');
}

public function guess($path)
{
    if (!is_file($path)) {
        throw new FileNotFoundException($path);
    }

    if (!is_readable($path)) {
        throw new AccessDeniedException($path);
    }

    ob_start();

    passthru(sprintf($this->cmd, escapeshellarg($path)), $return);
    if ($return > 0) {
        ob_end_clean();

        return;
    }

    $type = trim(ob_get_clean());
    if (!preg_match('#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i', $type, $match)) {
        // it's not a type, but an error message
        return;
    }

    return $match[1];
}
}

导入pptx文件时,mimetype命令返回application / zip。

在本地,mimetype返回正确的mimetype。看来,mime类型在导入时已被修改。    application / vnd.openxmlformats-officedocument.presentationml.presentation

您对此问题有解决方案吗?