123dok/file-type-detector

Detects file type by filename or content and generates correct mimetype.

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

This package is auto-updated.

Last update: 2024-12-10 17:04:56 UTC


README

Files type detector based on file name extension or file content (binary content).

Composer package Latest Stable Version Total Downloads Latest Unstable Version License Tests

  1. Usage
  2. Installation
  3. Supported formats

Usage

File Type detection

  • Detection by file name: Detector::detectByFilename(...filename...): array|boolean
  • Detection by file content or stream content: Detector::detectByContent(...filename/resource...): array|boolean

Both functions will return an array with following elements in case of success:

  • [0] - Type of file (Detector::AUDIO and so on)
  • [1] - Format of file (Detector::MP3 and so on)
  • [2] - Mime type of file ('audio/mpeg' for example)

In case of failure it will return false.

Example:

$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'));

Mimetype generation

To get correct mimetype for file only there is getMimeType($file) function.

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

Installation

Install package via composer:

composer require 123dok/file-type-detector

Supported formats

Available to use types and their formats.

Formats support status.