Skip to content

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

License

Notifications You must be signed in to change notification settings

BrandEmbassy/php-file-type-detector

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project forked from https://github.com/wapmorgan/FileTypeDetector

File Type Detector

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

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

Mimetype resolving

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

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

Installation

composer require brandembassy/file-type-detector

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%