Top 6 PHP Libraries for file manipulation and MIME type detection

In this post I am going to share Top 6 PHP Libraries for file manipulation and MIME type detection These libraries will help you to manage a lot of media files in your php based projects.

Top 6 PHP Libraries for file manipulation and MIME type detection

1. Gaufrette – A filesystem abstraction layer.

Imagine you have to manage a lot of medias in a PHP project. Lets see how to take this situation in your advantage using Gaufrette. The filesystem abstraction layer permits you to develop your application without the need to know where all those medias will be stored and how. Another advantage of this is the possibility to update the files location without any impact on the code apart from the definition of your filesystem. In example, if your project grows up very fast and if your server reaches its limits, you can easily move your medias in an Amazon S3 server or any other solution.

use Gaufrette\Filesystem;
use Gaufrette\Adapter\Local as LocalAdapter;

$adapter = new LocalAdapter('/var/media');
$filesystem = new Filesystem($adapter);

$filesystem->write('myFile', 'Hello world!');

$file = $filesystem->get('myFile');
echo sprintf('%s (modified %s): %s', $file->getKey(), date('d/m/Y, H:i:s', $file->getMtime()), $file->getContent());
// Will print something like: "myFile (modified 17/01/2016 18:40:36): Hello world!"


2. Canal – A library to determine internet media types.

Content analysis for the purpose of determining Internet media types.


$analyzer = new Dflydev\Canal\Analyzer\Analyzer;

$internetMediaType = $analyzer->detectFromFilename('/path/to/whatever.png');

print $internetMediaType->asString()."\n\n";

print $internetMediaType->getType()."\n\n";

print $internetMediaType->getSubtype()."\n\n";

// png

3. Apache MIME Types – A library that parses Apache MIME types

Parses Apache MIME Types files and provides a simple interface to find extensions by type and type by extension.

  • Bundles mime.types from the Apache HTTP Project. (see here)
  • Bundles a JSON representation of Apache mime.types.
  • Provides an interface for reading either flat Apache HTTP mime.types
    or a JSON representation.
$parser = new Dflydev\ApacheMimeTypes\Parser;
$map = $parser->parse('/path/to/mime.types');

4. Hoa Mime – Another MIME detection library.

Hoa is a modular, extensible and structured set of PHP libraries.
Moreover, Hoa aims at being a bridge between industrial and research worlds.
This library allows to manipulate a MIME types database and get some related informations about streams.

$type = new Hoa\Mime\Mime(new Hoa\File\Read('index.html'));

5. File Locator – A simple file locator library.

The FileLocator library was created to provide file locating capabilities to a larger project. It can be used in templating engines, configuration file loaders, and more.

The library includes FileSystemLocator, which can be used to search one or more directory paths. Either a single directory path, or an array of paths may be passed to the constructor.

use Herrera\FileLocator\Locator\FileSystemLocator;
$locator = new FileSystemLocator('/path/to/dir');
$locator = new FileSystemLocator(array(
$file = $locator->locate('file.ini'); // return the first "file.ini" found
$files = $locator->locate('file.ini', false); // find all named "file.ini"

6. PHP FFmpeg – An object oriented PHP driver for FFMpeg binary

This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it. Be sure that these binaries can be located with system PATH to get the benefit of the binary detection, otherwise you should have to explicitly give the binaries path on load.

require 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
    ->resize(new FFMpeg\Coordinate\Dimension(320, 240))
    ->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
    ->save(new FFMpeg\Format\Video\WMV(), 'export-wmv.wmv')
    ->save(new FFMpeg\Format\Video\WebM(), 'export-webm.webm');