123dok/file-type-detector

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

v1.1.11 2018-07-09 04:34 UTC

This package is auto-updated.

Last update: 2024-09-10 16:28:55 UTC


README

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

Composer package 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 = Dok123\FileTypeDetector\Detector::detectByFilename($filename);
// or
$type = Dok123\FileTypeDetector\Detector::detectByContent('file-without-extension');
// or
$type = Dok123\FileTypeDetector\Detector::detectByContent(fopen('http://somedomain/somepath', 'r'));

MIME类型生成

要获取文件的正确MIME类型,可以使用getMimeType($file)函数。

$mime = Dok123\FileTypeDetector\Detector::getMimeType($file);
// or
$mime = Dok123\FileTypeDetector\Detector::getMimeType(fopen('somefile', 'r'));

安装

通过composer安装包

composer require 123dok/file-type-detector

支持的格式

可用的类型及其格式。

格式支持状态。