brandembassy / file-type-detector
通过文件名或内容检测文件类型并生成正确的MIME类型。源自 https://github.com/wapmorgan/FileTypeDetector
2.3.2
2024-02-12 20:51 UTC
Requires
- php: ^7.2 || ^8.0
- marc-mabe/php-enum: ^3.1 || ^4.2
Requires (Dev)
- ext-zip: *
- brandembassy/coding-standard: ^8.1
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-12 21:59:24 UTC
README
项目源自 https://github.com/wapmorgan/FileTypeDetector
文件类型检测器
基于文件扩展名或文件内容(二进制内容)的文件类型检测器。
文件类型检测
- 通过文件名检测:
Detector::detectByFilename(...filename...): array|boolean
- 通过文件内容或流内容检测:
Detector::detectByContent(...filename/resource...): array|boolean
如果成功,这两个函数将返回一个包含以下元素的 array
[0]
- 文件类型(Detector::AUDIO
等)[1]
- 文件格式(Detector::MP3
等)[2]
- 文件MIME类型(例如'audio/mpeg'
)
如果失败,将返回 false
。
示例
$type = wapmorgan\FileTypeDetector\Detector::detectByFilename($filename); // or $type = wapmorgan\FileTypeDetector\Detector::detectByContent('file-without-extension'); // or $type = wapmorgan\FileTypeDetector\Detector::detectByContent(fopen('http://somedomain/somepath', 'r'));
MIME类型解析
要获取文件的正确MIME类型,可以使用 getMimeType($file)
函数。
$mime = wapmorgan\FileTypeDetector\Detector::getMimeType($file); // or $mime = wapmorgan\FileTypeDetector\Detector::getMimeType(fopen('somefile', 'r'));
安装
composer require brandembassy/file-type-detector