wapmorgan/file-type-detector

通过文件名或内容检测文件类型,并生成正确的MIME类型。

1.1.2 2017-05-13 11:45 UTC

This package is auto-updated.

Last update: 2024-08-29 04:37:31 UTC


README

基于文件扩展名或文件内容(二进制内容)的文件类型检测器。

Latest Stable Version Total Downloads Latest Unstable Version License Tests

  1. 使用方法
  2. 安装
  3. 支持的格式

使用方法

文件类型检测

  • 通过文件名检测: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安装包

composer require wapmorgan/file-type-detector

支持的格式

可用的类型及其格式。

格式支持状态。