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

Overview

FileTypeDetector

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 = 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 generation

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

Install package via composer:

composer require wapmorgan/file-type-detector

Supported formats

Available to use types and their formats.

Application Archive Audio Database Disk_image Document Feed Font Image Presentation Scenario Spreadsheet Video
apk 7z aac accdb iso doc atom otf bmp odp reg csv 3gp
com arc amr mdb nrg docx rss ttf gif ppt ods asf
exe arj flac odb vhd html ico pptx tsv avi
xap bzip2 m3u sqlite json jpeg xls flv
cab midi markdown png xlsx m4v
dar mp3 odt psd mkv
gzip ogg pdf tiff mov
jar wav rtf mp4
lzma2 wma txt mpeg
rar xml swf
tar yaml vob
zip webm
wmv

Formats support status.

Format Extension Detection by content MimeType Signature
3gp 3gp + video/3gpp at [0]: (0x0001466747970336770)
7z 7z + application/x-7z-compressed at [0]: (0x377abcaf271c)
Aac aac + audio/x-aac at [0]: (0xfff1) / at [0]: (0xfff9)
Accdb accdb + application/x-msaccess at [0]: (0x01005374616e6461726420414345204442)
Amr amr + audio/amr at [0]: (0x2321414d52)
Apk apk + application/vnd.android.package-archive at [0]: (0x504b34) & at [30]: ('AndroidManifest.xml')
Arc arc + application/x-freearc at [0]: (0x4172431)
Arj arj + application/arj at [0]: (0x60ea)
Asf asf - -
Atom atom + application/atom+xml at [0]: ('<?xml') & at [10]: (fuzzy search 'Atom')
Avi avi + video/x-msvideo at [0]: (0x52494646) & at [8]: (0x415649204c495354)
Bmp bmp + image/bmp at [0]: (0x424d)
Bzip2 bz2 + application/x-bzip2 at [0]: (0x425a68)
Cab cab + application/vnd.ms-cab-compressed at [0]: (0x4d534346)
Com com - application/x-msdownload
Csv csv - text/csv
Dar dar + application/x-dar at [0]: (0x0007b)
Doc doc + application/msword at [0]: (0xd0cf11e0a1b11ae1) & at [512]: (0xeca5c10)
Docx docx + application/vnd.openxmlformats-officedocument.wordprocessingml.document at [0]: (0x504b3414060) & at [-22]: (fuzzy search 'word/')
Exe exe + application/x-msdownload at [0]: (0x4d5a)
Flac flac + audio/x-flac at [0]: (0x664c614300022)
Flv flv + video/x-flv at [0]: (0x464c561)
Gif gif + image/gif at [0]: (0x474946383761) / at [0]: (0x474946383961)
Gzip gz + application/gzip at [0]: (0x1f8b)
Html html + text/html at [0]: ('<html')
Ico ico + image/x-icon at [0]: (0x0010)
Iso iso + application/x-iso9660-image at [0]: (0x4344303031)
Jar jar + application/java-archive at [0]: (0x504b341408080) / at [0]: (0x5f27a889)
Jpeg jpeg + image/jpeg at [0]: (0xffd8ffe0)
Json json - application/json
Lzma2 xz - application/x-xz
M3u m3u + audio/x-mpegurl at [0]: ('#EXTM3U')
M4v m4v + video/x-m4v at [0]: (0x00018667479706d703432)
Markdown md - text/markdown
Mdb mdb + application/x-msaccess at [0]: (0x01005374616e64617264204a6574204442)
Midi midi + audio/midi at [0]: (0x4d546864)
Mkv mkv + video/x-matroska at [0]: (0x1a45dfa3934282886d6174726f736b61)
Mov mov + video/quicktime at [4]: (0x6674797071742020) / at [4]: (0x6d6f6f76)
Mp3 mp3 + audio/mpeg at [0]: (0x494433)
Mp4 mp4 + video/mp4 at [4]: (0x6674797069736f6d) / at [4]: (0x6674797033677035) / at [4]: (0x667479704d534e56) / at [4]: (0x667479704d344120)
Mpeg mpeg + video/mpeg at [0]: (0x001) & at [-4]: (0x001b7)
Nrg nrg + - at [-8]: ('NERO') / at [-12]: ('NER5')
Odb odb + application/vnd.oasis.opendocument.database at [0]: (0x504b34) & at [30]: ('mimetypeapplication/vnd.oasis.opendocument.') & at [73]: ('base')
Odp odp + application/vnd.oasis.opendocument.presentation at [0]: (0x504b34) & at [30]: ('mimetypeapplication/vnd.oasis.opendocument.') & at [73]: ('presentation')
Ods ods + application/vnd.oasis.opendocument.spreadsheet at [0]: (0x504b34) & at [30]: ('mimetypeapplication/vnd.oasis.opendocument.') & at [73]: ('spreadsheet')
Odt odt + application/vnd.oasis.opendocument.text at [0]: (0x504b34) & at [30]: ('mimetypeapplication/vnd.oasis.opendocument.') & at [73]: ('text')
Ogg ogg + audio/ogg at [0]: ('OggS')
Otf otf + application/x-font-otf at [0]: (0x4f54544f)
Pdf pdf + application/pdf at [0]: (0x25504446)
Png png + image/png at [0]: (0x89504e47da1aa)
Ppt ppt + application/vnd.ms-powerpoint at [0]: (0xd0cf11e0a1b11ae1) & at [512]: (0xa0461df0) / at [0]: (0xd0cf11e0a1b11ae1) & at [512]: (0x06e1ef0) / at [0]: (0xd0cf11e0a1b11ae1) & at [512]: (0xf0e83)
Pptx pptx + application/vnd.openxmlformats-officedocument.presentationml.presentation at [0]: (0x504b3414060) & at [-22]: (fuzzy search 'ppt/')
Psd psd + image/vnd.adobe.photoshop at [0]: (0x38425053)
Rar rar + application/x-rar-compressed at [0]: (0x526172211a70) / at [0]: (0x526172211a710)
Reg reg + text/plain at [0]: (0xfffe) / at [0]: (0x52454745444954)
Rss rss + application/rss+xml at [0]: ('<?xml') & at [10]: (fuzzy search '<rss')
Rtf rtf + application/rtf at [0]: (0x7b5c72746631)
Swf swf + application/x-shockwave-flash at [0]: (0x5a5753)
Tar tar + application/x-tar at [0]: (0x757374617203030) / at [0]: (0x757374617220200)
Tiff tiff + image/tiff at [0]: (0x492049) / at [0]: (0x49492a0) / at [0]: (0x4d4d02a) / at [0]: (0x4d4d02b)
Tsv tsv - text/tab-separated-values
Ttf ttf + application/x-font-ttf at [0]: (0x01000)
Txt txt - text/plain
Vhd vhd - -
Vob vob + video/x-ms-vob at [0]: (0x001ba) & at [-4]: (0x001b9)
Wav wav - audio/x-wav
Webm webm + video/webm at [0]: (0x1a45dfa3)
Wma wma - audio/x-ms-wma
Wmv wmv - video/x-ms-wmv
Xap xap - application/x-silverlight-app
Xls xls + application/vnd.ms-excel at [0]: (0xd0cf11e0a1b11ae1) & at [512]: (0x981000650)
Xlsx xlsx + application/vnd.openxmlformats-officedocument.spreadsheetml.sheet at [0]: (0x504b3414060) & at [-22]: (fuzzy search 'xl/')
Xml xml + application/xml at [0]: ('<?xml')
Yaml yaml - text/yaml
Zip zip + application/zip at [0]: (0x504b34) / at [0]: (0x504b56) / at [0]: (0x504b78)
You might also like...
File manager module for the Lumen PHP framework.

Lumen File Manager File manager module for the Lumen PHP framework. Please note that this module is still under active development. NOTE: Branch 5.1 i

A web based file manager,web IDE / browser based code editor
A web based file manager,web IDE / browser based code editor

KodExplorer Update to kodbox: https://github.com/kalcaddle/kodbox Download | Demo It is recommended to use a new design upgrade product:kodbox 该项目处于维护

🛬🧾 A PHP8 TacView ACMI file format parser

A PHP8 TacView ACMI file format parser This package offers parsing support for TacView ACMI flight recordings version 2.1, it follows the standard des

Upload File Library For PHP ( Simple & Easy User )

Upload File Library For Backend/ServerSide PHP ( Simple & Easy For Use ), Support Multiple Upload

Creating a file uploader with Laravel
Creating a file uploader with Laravel

File-Uploader File uploader is a simple group of restful APIs built in Laravel to upload file into a server and remove the file from that server. Tool

Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP.

Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP. When you use this package, you're protected from vendor lock-in, That mean you can connect to any FTP/SFTP storage. Also it's comes with base URL option to connect Gotipath CDN.

Simple file uploading, Pomf like

Pomf-0x80 Simple file uploading, Pomf like. Features One click uploading, no registration required IPFS Upload + Pin A minimal, modern web interface D

PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats

PHPProject PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats, i.e. Micr

FileNamingResolver - A lightweight library which helps to resolve a file/directory naming of uploaded files using various naming strategies

FileNamingResolver - A lightweight library which helps to resolve a file/directory naming of uploaded files using various naming strategies

Comments
  • 1.1.0 is broken?

    1.1.0 is broken?

    Hello, im using 1.0.5 but in composer i have ^1.0 and yesterday when you push new 1.1.0 tag - tests on my package not passing, because your package have issues. first to gen mime im using implode('/', $result) and in 1.0.5 is work = image/png but in 1.1.0 result is image/png/image/png

    and https://github.com/wapmorgan/FileTypeDetector/blob/master/src/Detector.php#L561 $file is undefined

    opened by errogaht 3
  • jpeg/exif support

    jpeg/exif support

    Hi, I found that the detector does not recognize all jpeg because some use the header FFD8FFE1 instead of FFD8FFE0.

    I found an explanation here: https://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_JPEG_files#21-Exif

    $ file FA.jpg 
    FA.jpg: JPEG image data, Exif standard: [TIFF image data, little-endian, direntries=0], progressive, precision 8, 1200x600, components 3
    $ xxd FA.jpg 
    00000000: ffd8 ffe1 0018 4578 6966 0000 4949 2a00  ......Exif..II*.
    00000010: 0800 0000 0000 0000 0000 0000 ffec 0011  ................
    00000020: 4475 636b 7900 0100 0400 0000 3c00 00ff  Ducky.......<...
    00000030: e104 6368 7474 703a 2f2f 6e73 2e61 646f  ..chttp://ns.ado
    00000040: 6265 2e63 6f6d 2f78 6170 2f31 2e30 2f00  be.com/xap/1.0/.
    

    https://en.wikipedia.org/wiki/List_of_file_signatures Wikipedia also lists a jpeg raw header

    opened by black-silence 0
Releases(1.1.2)
Analyze content to determine the appropriate Internet media type

Canal Content analysis for the purpose of determining Internet media types. Requirements PHP 5.3+ Installation Through Composer as dflydev/canal. Usag

dflydev 34 Feb 11, 2022
FIle Uploader is a php package to aid fast , easy and safe file upload

FILE UPLOADER FIle Uploader is a php package to aid fast , easy and safe file upload installation composer require codad5/file-helper Features Fast an

Aniezeofor Chibueze Michael 2 Sep 3, 2022
Private file storage and share with user build with laravel and vue inspired by google drive

LaravelDrive is a file storage system that allows store private file and share with users build wiht laravel and vue inspired by google drive. Laravel

Shahadat Hossain 70 Dec 22, 2022
Uguu is a simple lightweight temporary file host with support for drop, paste, click and API uploading.

Uguu What is Uguu? Uguu is a simple lightweight temporary file hosting and sharing platform, but can also be used as a permanent file host. Features O

Eric Johansson (neku) 547 Dec 28, 2022
Basic anonymous and registered upload storage for temporary share file self hosted.

TMPShareX Basic anonymous and registered upload storage for temporary share file self hosted. Server Requirement PHP 7.4.8 [Support PHP 8] Nginx 1.19.

Sandy Hermansyah 1 Feb 3, 2022
Media gallery with CKEditor, TinyMCE and Summernote support. Built on Laravel file system.

Documents ・ Installation ・ Integration ・ Config ・ Customization ・ Events ・ Upgrade ・ Demo ・ FAQ Features File upload and management Uploading validati

UniSharp 1.9k Jan 1, 2023
A lightweight file manager with full ShareX, Screencloud support and more

XBackBone is a simple, self-hosted, lightweight PHP file manager that support the instant sharing tool ShareX and *NIX systems. It supports uploading

Sergio Brighenti 751 Jan 8, 2023
Pomf is a simple lightweight file host with support for drop, paste, click and API uploading.

Pomf Pomf is a simple file uploading and sharing platform. Features One click uploading, no registration required A minimal, modern web interface Drag

Pomf 772 Jan 8, 2023
kodbox is a file manager for web. It is a newly designed product based on kodexplorer.

kodbox is a file manager for web. It is a newly designed product based on kodexplorer. It is also a web code editor, which allows you to develop websites directly within the web browser.You can run kodbox either online or locally,on Linux, Windows or Mac based platforms

warlee 1.2k Jan 7, 2023
Watch changes in the file system using PHP

Watch changes in the file system using PHP This package allows you to react to all kinds of changes in the file system. Here's how you can run code wh

Spatie 170 Dec 25, 2022