定制 adrienrn/php-mimetyper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

adrienrn/php-mimetyper

Composer 安装命令:

composer require adrienrn/php-mimetyper

包简介

PHP mime type and extension mapping library: compatible with Symfony, powered by jshttp/mime-db

README 文档

README

PHP mime type and extension mapping library: built with jshttp/mime-db, compatible with Symfony and Laravel.

use MimeTyper\Repository\MimeDbRepository;

$mimeRepository = new MimeDbRepository();

$mimeRepository->findExtensions("image/jpeg"); // ["jpeg","jpg","jpe"]
$mimeRepository->findExtension("image/jpeg"); // "jpeg"

$mimeRepository->findType("html"); // "html"
$mimeRepository->findType("js"); // 'application/javascript'

The most complete and up-to-date mime type mapping for PHP!

The goal is to provide a complete and up-to-date mime types mapping for PHP and build a comprehensive and simple interface for PHP. This package is heavily inspired from dflydev work and extends it.

Mime types mapping, the right way.

This library uses jshttp/mime-db as its default mapping which aggregates data from multiple sources and creates a single db.json making it the most complete two ways mapping, from mime to extension and extension to mime types too.

Custom mime types and custom repositories

Some custom types (aliases really) are maintained locally too, in the same JSON format as jshttp/mime-db.

use MimeTyper\Repository\ExtendedRepository;

$mimeRepostory = new ExtendedRepository();

$mimeRepository->findExtensions("text/x-php"); // ["php", "php2", "php3", "php4", "php5"]

$mimeRepository->findTypes("php"); // ["text/x-php", "application/x-php", "text/php", "application/php", "application/x-httpd-php"]
$mimeRepository->findType("php"); // "text/x-php"

The reason to maintain aliases locally helps with overall compatibility between mime type guessing methods. Tools detecting mime types don't always return standard mime type or the standard mime type does not exist. All of those custom mime types might be added to jshttp/mime-db custom types in the end.

Example: Debian will detect a PHP file as text/x-php while browsers will send application/x-httpd-php. It goes the same with files such as Javascript (application/javascript vs text/javascript) or Microsoft Office / Libre Office files.

Don't hesitate to make a pull request to discuss this.

Mime types for Symfony and Laravel

This library is compatible with your Symfony or Laravel app to enjoy the completeness of the mapping.

Use the ExtraMimeTypeExtensionGuesser as a bridge class between Symfony ExtensionGuesser and this package RepositoryInterface.

use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;

use MimeTyper\Repository\ExtendedRepository;
use MimeTyper\Symfony\ExtraMimeTypeExtensionGuesser;

$symfonyGuesser = ExtensionGuesser::getInstance();
$extraGuesser = new ExtraMimeTypeExtensionGuesser(
    new ExtendedRepository()
);
$symfonyGuesser->register($extraGuesser);

This example uses the ExtendedRepository (mime-db and local custom mime types), you can use the default MimeDbRepository, implement your own or use a CompositeRepository to aggregate multiple repostories.

Safe detection of mime type in PHP

Before mapping type to extension or extension to type, you need to be able to properly detect the mime type of a file.

For security reasons, do not trust browsers, eg $_FILES['your_file']['type'], when it comes to detect the mime type of a file.

To safely detect the mime type of a file, . Symfony is giving a great example with their MimeTypeGuesser implementation of:

It all ends up inspecting the file using finfo and relies on magic db files. PHP will use its own magic db or your system magic db depending on your environement.

Other PHP libraries for mime types

  • dflydev/dflydev-apache-mime-types

    Uses mime.types Apache file, comprehensive api. As stated before, php-mimetyper is heavily inspired by this, extending it to be a bit more complete using an external mapping and a wider interface.

  • symfony/http-foundation

    Symfony provides a nice interface for guessing mime types and extensions but uses only a local mapping based on Apache registry, see above to bridge it to this package.

  • davidpersson/mm

    Library for media processing and mime type and extension guessing. Uses FreeDesktop magic.db file for the latter.

  • Hoa/Mime

    The Hoa package to deal with mime types. Uses mime.types Apache file (local fallback) and relies on static methods.

  • karwana/php-mime

    Uses mime.types Apache file and finfo, requires PHP >=5.4.

  • PEAR/MIME_Type

    Detect the mime type of a file: uses internally finfo_file, mime_content_type or file command to guess the mime type.

adrienrn/php-mimetyper 适用场景与选型建议

adrienrn/php-mimetyper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 828.65k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2016 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 adrienrn/php-mimetyper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adrienrn/php-mimetyper 我们能提供哪些服务?
定制开发 / 二次开发

基于 adrienrn/php-mimetyper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 828.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 17
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-12