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.

<?php
 
use Gaufrette\Filesystem;
use Gaufrette\Adapter\Local as LocalAdapter;
 
// First, you need a filesystem adapter
$adapter = new LocalAdapter('/var/media');
$filesystem = new Filesystem($adapter);
 
// Then, you can access your filesystem directly
var_dump($filesystem->read('myFile')); // bool(false)
$filesystem->write('myFile', 'Hello world!');
 
// Or use File objects
$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!"
 
// You can also rename your file like this:
$file->rename('my/new/file');

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

Content analysis for the purpose of determining Internet media types.

<?php
 
// Instantiate the Analyzer
$analyzer = new Dflydev\Canal\Analyzer\Analyzer;
 
// Detect a media type from a filename (file does not need to exist)
$internetMediaType = $analyzer->detectFromFilename('/path/to/whatever.png');
 
// See the media type as a string
print $internetMediaType->asString()."\n\n";
 
// See the media type's type
print $internetMediaType->getType()."\n\n";
 
// See the media type's subtype
print $internetMediaType->getSubtype()."\n\n";
 
// image/png
//
// image
//
// 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.
Features:

  • 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.
<?php
$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'));
 
var_dump(
    $type->getExtension(),
    $type->getOtherExtensions(),
    $type->getMime(),
    $type->isExperimental()
);
 
/**
 * Will output:
 *     string(4) "html"
 *     array(1) {
 *       [0]=>
 *       string(3) "htm"
 *     }
 *     string(9) "text/html"
 *     bool(false)
 */



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.

<?php
use Herrera\FileLocator\Locator\FileSystemLocator;
 
$locator = new FileSystemLocator('/path/to/dir');
$locator = new FileSystemLocator(array(
    '/path/to/dir1',
    '/path/to/dir2',
    '/path/to/dir3' // etc
));
 
$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');
$video
    ->filters()
    ->resize(new FFMpeg\Coordinate\Dimension(320, 240))
    ->synchronize();
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame.jpg');
$video
    ->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');