首页>开发手册>Symfony>Working with MIME Types
Symfony 手册目录展开/收起

Symfony 7.4 LTS

Working with MIME Types

本文目录展开

官方英文资料(未自动翻译);本文按官方对应版本整理,保留说明和代码。站内处理包括移除站点导航、还原代码格式及调整链接。

MIME (Multipurpose Internet Mail Extensions) is an Internet standard that was designed to extend the format of emails. The content types defined by MIME standards (also known as MIME types and "media types") are also important in communication protocols outside of email, such as HTTP. That's why Symfony provides utilities to work with MIME types via its Mime component.

See also

This article explains the MIME type utilities of the Mime component. To create and send email messages, read the Mailer article.

Installation

The Mime component is already installed in applications that use the Mailer. In any other application, run this command to install it:

$ composer require symfony/mime

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Converting MIME Types into File Extensions

The MimeTypes class transforms between MIME types and file name extensions. In Symfony applications, inject this object into your services using the MimeTypesInterface type-hint; in any other application, create it yourself:

// src/Service/SomeService.php
namespace App\Service;

use Symfony\Component\Mime\MimeTypesInterface;

class SomeService
{
    public function __construct(
        private MimeTypesInterface $mimeTypes,
    ) {
    }

    // ...
}
use Symfony\Component\Mime\MimeTypes;

$mimeTypes = new MimeTypes();

Use the getExtensions() and getMimeTypes() methods to transform between MIME types and file name extensions:

$exts = $mimeTypes->getExtensions('application/javascript');
// $exts = ['js', 'jsm', 'mjs']
$exts = $mimeTypes->getExtensions('image/jpeg');
// $exts = ['jpeg', 'jpg', 'jpe']

$types = $mimeTypes->getMimeTypes('js');
// $types = ['application/javascript', 'application/x-javascript', 'text/javascript']
$types = $mimeTypes->getMimeTypes('apk');
// $types = ['application/vnd.android.package-archive']

These methods return arrays with one or more elements. The element position indicates its priority, so the first returned extension is the preferred one.

Guessing the MIME Type of a File

Another useful utility lets you guess the MIME type of any given file:

$mimeType = $mimeTypes->guessMimeType('/some/path/to/image.gif');
// Guessing is not based on the file name, so $mimeType will be 'image/gif'
// only if the given file is truly a GIF image

Guessing the MIME type is a time-consuming process that requires inspecting part of the file contents. Symfony applies multiple guessing mechanisms, one of them based on the PHP fileinfo extension. It's recommended to install that extension to improve the guessing performance.

To validate the MIME type of uploaded files, use the File constraint.

Adding a MIME Type Guesser

You can add your own MIME type guesser by creating a class that implements MimeTypeGuesserInterface:

namespace App;

use Symfony\Component\Mime\MimeTypeGuesserInterface;

class SomeMimeTypeGuesser implements MimeTypeGuesserInterface
{
    public function isGuesserSupported(): bool
    {
        // return true when the guesser is supported (might depend on the OS for instance)
        return true;
    }

    public function guessMimeType(string $path): ?string
    {
        // inspect the contents of the file stored in $path to guess its
        // type and return a valid MIME type ... or null if unknown

        return '...';
    }
}

In Symfony applications, MIME type guessers must be registered as services and tagged with the mime.mime_type_guesser tag. If you're using the default services.yaml configuration, this is already done for you, thanks to autoconfiguration.

In any other application, register the guesser explicitly (the most recently registered guessers take precedence):

$mimeTypes->registerGuesser(new SomeMimeTypeGuesser());
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
Version

来源与许可

Symfony documentation contributors。本资料采用 CC BY-SA 3.0;本页为官方资料的站内整理版本,并非框架官方网站。 许可说明

Symfony 官方文档 · 原文来源 · 打开在线代码运行