wwaz/mimetype-php
Composer 安装命令:
composer require wwaz/mimetype-php
包简介
Determines mimetype of a filepath (string) or file resource
README 文档
README
Zero-config MIME type detection for PHP – automatically via file extension or content type.
Quick Start
composer require wwaz/mimetype
use wwaz\Mimetype\Mimetype; echo Mimetype::get('document.pdf'); // → application/pdf
get() first tries detection by file extension. If that fails (missing extension or unknown type), it automatically falls back to content-type analysis – no configuration needed.
API
Mimetype::get(string $filename): string|false
Universal method – the right choice for most cases.
Mimetype::get('image.png'); // → image/png Mimetype::get('/var/upload/avatar'); // → image/jpeg (via content type) Mimetype::get('archive.tar.gz'); // → application/gzip
Mimetype::fromPath(string $filename): string|false
Detection by file extension only. Throws MimetypeException if the extension is missing.
Mimetype::fromContentType(string $filename): string|false
Detection by PHP mime_content_type() only. The file must exist on the filesystem.
Examples
1 – Validate a file upload
$mime = Mimetype::get($_FILES['upload']['tmp_name']); $allowed = ['image/jpeg', 'image/png', 'image/webp']; if (!in_array($mime, $allowed)) { throw new RuntimeException('Only JPEG, PNG and WebP are allowed.'); }
2 – Set the Content-Type header
$file = '/var/www/assets/logo.svg'; header('Content-Type: ' . Mimetype::get($file)); readfile($file);
3 – Handle a missing extension gracefully
use wwaz\Mimetype\Exceptions\MimetypeException; try { $mime = Mimetype::fromPath('mysterious-file'); } catch (MimetypeException $e) { // Fall back to content-type detection $mime = Mimetype::fromContentType('/path/to/mysterious-file'); }
Dependencies
| Package | Purpose |
|---|---|
| ralouphie/mimey | Extensible MIME type database |
| PHP ≥ 8.0 | `string |
MIT © wwaz
wwaz/mimetype-php 适用场景与选型建议
wwaz/mimetype-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wwaz/mimetype-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wwaz/mimetype-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 42
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-12