jshttp/mime-db
The canonical MIME type database from jshttp, published as JSON. It maps file extensions to media types and includes metadata such as compression and charset, powering Node.js HTTP libraries like mime-types and Express.
Installation
composer require jshttp/mime-db
The package is dependency-free and requires no configuration—just autoload it via Composer.
First Use Case: Detecting MIME Types
use JSHttp\MimeDb\MimeDb;
$mimeDb = new MimeDb();
$mimeType = $mimeDb->getMimeType('application/json'); // Returns 'application/json'
$extension = $mimeDb->getExtension('application/json'); // Returns 'json'
Where to Look First
JSHttp\MimeDb\MimeDb (main interface for lookups).MIME Type Lookup
$mimeDb = new MimeDb();
$type = $mimeDb->getMimeType('txt'); // 'text/plain'
Extension-to-MIME Mapping
$extension = $mimeDb->getExtension('application/pdf'); // 'pdf'
Content-Disposition headers or validating file types.Reverse Lookup (MIME → Extension)
$mimeDb->getExtension('image/png'); // 'png'
Integration with Laravel Filesystem
use Illuminate\Support\Facades\Storage;
$path = 'example.pdf';
$mime = $mimeDb->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
Storage::disk('s3')->put($path, file_get_contents($path), ['mime_type' => $mime]);
Custom MIME Types
$mimeDb->addType('application/vnd.myapp+json', ['json']);
$mimeDb->addExtension('myjson', 'application/vnd.myapp+json');
Cache::remember) for performance.
$mime = Cache::remember("mime_{$extension}", 3600, fn() => $mimeDb->getMimeType($extension));
$mime = $mimeDb->getMimeType($extension) ?: 'application/octet-stream';
MimeDb as a singleton for global access:
$this->app->singleton(MimeDb::class, fn() => new MimeDb());
Case Sensitivity
$mimeDb->getMimeType(strtolower($extension));
Missing Types
application/vnd.custom), add them manually:
$mimeDb->addType('application/vnd.custom', ['custom']);
Performance
MimeDb instance is reused (e.g., via a service container).Overwriting Defaults
addType() and addExtension() modify the instance’s state. If using a singleton, changes persist globally. Clone the instance if isolation is needed:
$customDb = clone $mimeDb;
$customDb->addType('...');
dd($mimeDb->getAllTypes()); // Array of all registered MIME types.
getAllExtensions() to list all supported extensions and cross-reference with your inputs.JSHttp\MimeDb\MimeDb to load from a custom file or API:
class CustomMimeDb extends MimeDb {
public function __construct() {
parent::__construct();
$this->loadFromArray($customTypesArray);
}
}
$mimeDb->addType('...');
event(new MimeTypeAdded($mimeDb));
Content-Type headers:
$response->header('Content-Type', $mimeDb->getMimeType($extension));
How can I help you explore Laravel packages today?