An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

        

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

[![Latest Stable Version](https://poser.pugx.org/wapmorgan/file-type-detector/v/stable)](https://packagist.org/packages/wapmorgan/file-type-detector)
[![Total Downloads](https://poser.pugx.org/wapmorgan/file-type-detector/downloads)](https://packagist.org/packages/wapmorgan/file-type-detector)
[![Latest Unstable Version](https://poser.pugx.org/wapmorgan/file-type-detector/v/unstable)](https://packagist.org/packages/wapmorgan/file-type-detector)
[![License](https://poser.pugx.org/wapmorgan/file-type-detector/license)](https://packagist.org/packages/wapmorgan/file-type-detector)
[![Tests](https://travis-ci.org/wapmorgan/FileTypeDetector.svg?branch=master)](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]: ('