All Projects → SoftCreatR → php-mime-detector

SoftCreatR / php-mime-detector

Licence: ISC license
Detect a file's mime type using magic numbers.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-mime-detector

magic-bytes
A library for detecting file types.
Stars: ✭ 20 (+0%)
Mutual labels:  detection, file, file-type
generator-composer
🐘 Yeoman (http://yeoman.io) generator for a PHP Composer project
Stars: ✭ 16 (-20%)
Mutual labels:  composer, phpunit
routing
Aplus Framework Routing Library
Stars: ✭ 186 (+830%)
Mutual labels:  composer, php8
app
Aplus Framework App Project
Stars: ✭ 338 (+1590%)
Mutual labels:  composer, php8
darknet
php ffi darknet
Stars: ✭ 21 (+5%)
Mutual labels:  detection, php8
session
Aplus Framework Session Library
Stars: ✭ 170 (+750%)
Mutual labels:  composer, php8
alpine-php-fpm
Lightweight and optimised PHP-FPM (PHP 7.4, 8.0, 8.1) Docker images with essential extensions on top of latest Alpine Linux.
Stars: ✭ 53 (+165%)
Mutual labels:  composer, php8
Pdt
PHP Development Tools project (PDT)
Stars: ✭ 135 (+575%)
Mutual labels:  composer, phpunit
autoload
Aplus Framework Autoload Library
Stars: ✭ 18 (-10%)
Mutual labels:  composer, php8
safe-svg
Enable SVG uploads and sanitize them to stop XML/SVG vulnerabilities in your WordPress website.
Stars: ✭ 129 (+545%)
Mutual labels:  file, mime
email
Aplus Framework Email Library
Stars: ✭ 127 (+535%)
Mutual labels:  composer, php8
database
Aplus Framework Database Library
Stars: ✭ 147 (+635%)
Mutual labels:  composer, php8
repair
ระบบบันทึกข้อมูลงานซ่อม
Stars: ✭ 24 (+20%)
Mutual labels:  php53, php8
pagination
Aplus Framework Pagination Library
Stars: ✭ 167 (+735%)
Mutual labels:  composer, php8
GCMS
PHP FASTEST CMS with Ajax support
Stars: ✭ 19 (-5%)
Mutual labels:  php53, php8
laravel-survey
Laravel 6 survey app.
Stars: ✭ 39 (+95%)
Mutual labels:  composer, phpunit
http
Aplus Framework HTTP Library
Stars: ✭ 113 (+465%)
Mutual labels:  composer, php8
Cloudreve
🌩支持多家云存储的云盘系统 (Self-deployed file management and sharing system, supports multiple storage providers)
Stars: ✭ 12,318 (+61490%)
Mutual labels:  composer, file
Designpattern
设计模式
Stars: ✭ 66 (+230%)
Mutual labels:  composer, phpunit
php8-xdebug
PHP 8.0 for development and production usage. With nginx, brotli, xdebug, JIT and more...
Stars: ✭ 17 (-15%)
Mutual labels:  composer, php8

PHP Mime Detector

Detecting the real type of (binary) files doesn't have to be hard. Checking a file's extension is not reliable and can cause serious security issues.

This package helps you to determine the correct type of files, by reading it byte for byte (up to 4096) and check for magic numbers.

However, this package isn't a replacement for any security software. It just aims to produce less false positives, than a simple extension check would produce.

A list of supported file types can be found on this Wiki page.

Why a separate class?

You may wonder why we don't just rely on extensions like Fileinfo? First off all, a short background story:

We are building extensions and applications for an Open Source PHP Framework, thus we are creating web software for the masses. Many of our customers and/or users of our free products are on shared hosting without any possibility to install or manage installed PHP extensions. So our goal is to develop solutions with as few dependencies as necessary, but with as much functionality as possible.

While developing a solution, that allows people to convert HEIF/HEIC files to a more "standardized" format (using our own, external API), we had troubles detecting these files, because especially this format isn't known by most Webservers, yet. While checking the file extension isn't reliable, we had to find a reusable solution that works for most of our clients. So we started to build a magic number based check for these files. That was the birth of our Mime Detector.

Why are the unit tests so poorly written?

Short answer: I have just little experience in unit testing. This project was a good training and even if the unit tests could be better: I am proud of my progress :)

Demo

A demo (based on dev-master) can be found at WhatTheFile.info.

Requirements

If you are looking for a solution that works on older PHP versions (5.3.2+), head over to the oldphp branch.

Installation

Require this package using Composer, in the root directory of your project:

$ composer require softcreatr/php-mime-detector

Usage

Here is an example on how this package makes it very easy to determine the mime type (and it's corresponding file extension) of a given file:

use SoftCreatR\MimeDetector\MimeDetector;
use SoftCreatR\MimeDetector\MimeDetectorException;

// create an instance of the MimeDetector
$mimeDetector = new MimeDetector();

// set our file to read
try {
    $mimeDetector->setFile('foo.bar');
} catch (MimeDetectorException $e) {
    die('An error occurred while trying to load the given file.');
}

// try to determine it's mime type and the correct file extension
$fileData = $mimeDetector->getFileType();

// print the result
echo '<pre>' . print_r($fileData, true) . '</pre>';

Or short:

use SoftCreatR\MimeDetector\MimeDetector;
use SoftCreatR\MimeDetector\MimeDetectorException;

try {
    echo '<pre>' . print_r((new MimeDetector())->setFile('foo.bar')->getFileType(), true) . '</pre>';
} catch (MimeDetectorException $e) {
    die('An error occurred while trying to load the given file.');
}

Testing

Testing utilizes PHPUnit (what else?) by running this command:

$ composer test

However, you may check out a bunch of test files for a full test. Test files are no longer included in the composer package nor the Git repository itself, so you have to perform a checkout of this repository and install it's submodules:

$ git clone https://github.com/SoftCreatR/php-mime-detector
$ cd php-mime-detector
$ git submodule update --init --recursive

When done, perform a composer install and run PHPUnit as described above.

ToDo

  • Reduce method sizes, when possible
  • Add a method, that accepts a mime type and returns the corresponding file extension
  • Add a method, that accepts a file extension and returns a list of corresponding mime types
  • Add a method, that returns a list of all detectable mime types and their corresponding file extensions

Contributing

Please see CONTRIBUTING for details.

When adding new detections, please make sure to provide at least one sample file.

License

ISC

Free Software, Hell Yeah!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].