https://github.com/wapmorgan/filetypedetector
Detects file type by filename or content and generates correct mimetype.
https://github.com/wapmorgan/filetypedetector
detection files format mimetype
Last synced: about 2 months ago
JSON representation
Detects file type by filename or content and generates correct mimetype.
- Host: GitHub
- URL: https://github.com/wapmorgan/filetypedetector
- Owner: wapmorgan
- License: mit
- Created: 2015-09-06T22:02:29.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-02-12T14:30:10.000Z (about 1 year ago)
- Last Synced: 2024-10-12T06:54:29.923Z (7 months ago)
- Topics: detection, files, format, mimetype
- Language: PHP
- Homepage: http://wapmorgan.github.io/FileTypeDetector/
- Size: 85 KB
- Stars: 33
- Watchers: 6
- Forks: 14
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FileTypeDetector
Files type detector based on file name extension or file content (binary content).[](https://packagist.org/packages/wapmorgan/file-type-detector)
[](https://packagist.org/packages/wapmorgan/file-type-detector)
[](https://packagist.org/packages/wapmorgan/file-type-detector)
[](https://packagist.org/packages/wapmorgan/file-type-detector)
[](https://travis-ci.org/wapmorgan/FileTypeDetector)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:
```php
$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.
```php
$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]: ('