Skip to content
/ mime Public

PHP implementation of the XDG Shared MIME-info Database standard.

License

Notifications You must be signed in to change notification settings

php-xdg/mime

Folders and files

NameName
Last commit message
Last commit date
Mar 4, 2024
Mar 4, 2024
Oct 3, 2022
Feb 3, 2023
Dec 28, 2023
Apr 8, 2024
Dec 28, 2023
Jun 12, 2022
Feb 3, 2023
Feb 3, 2023
Feb 3, 2023
Jun 12, 2022
Jun 20, 2022
Oct 3, 2022
Feb 5, 2023
Mar 4, 2024

Repository files navigation

xdg/mime

codecov

This package is a pure PHP implementation of the XDG Shared MIME-Info Database specification.

It is primarily aimed at MIME-type detection.

Installation

$ composer require xdg/mime

Usage

Implementation notes

Most XDG Shared MIME-Info implementations read their data from binary cache files generated by the update-mime-database program. This turns out to be impractical in the context of PHP for several reasons:

  • First, PHP doesn't have native mmap functionality, so reading these files at runtime can produce a lot of I/O.
  • Second, parsing these files at runtime has a non-negligible overhead.
  • And last, PHP runs on server environments where the update-mime-database program is often not available (and possibly even not installable).

Therefore, this library takes another approach: parsing the MIME database in its original XML format, and compiling it to executable PHP code. This yields several advantages:

  • Since most PHP applications have a build / deploy step, the overhead of parsing the database at runtime is eliminated entirely.
  • Generating PHP code allows leveraging the PHP opcode cache, which makes loading the database very fast.
  • Since performance is less paramount at build time, the library does additional work to optimize the generated PHP code for optimal runtime performance.

For convenience, this package ships with a pre-compiled database, built from the latest shared-mime-info database.