jstewmc/validate-file-type

Validate a file's type

dev-master 2016-09-06 18:44 UTC

This package is auto-updated.

Last update: 2024-04-29 03:10:15 UTC


README

Validate a file's mime-type.

use Jstewmc\ValidateFileType\Validate;

// create a txt file
$txtFilename = '/path/to/foo.txt';
$txtContents = 'foo';

file_put_contents($txtFilename, $txtContents);

// create an xml file
$xmlFilename = '/path/to/foo.xml';
$xmlContents = '<?xml version="1.0" encoding="UTF-8"?><foo />';

file_put_contents($xmlFilename, $xmlContents);

// create a service to validate text files
$service = new Validate(['text/plain']);

// validate our two files
$service($txtFilename);  // returns true
$service($xmlFilename);  // returns false

This library uses PHP's fileinfo extension to look for certain magic byte sequences in a file to guess it's type. While this is not a bulletproof approach, the heuristics used do a very good job.

That's it!

Author

Jack Clayton

License

MIT

Version

0.1.0, August 25, 2016

  • Initial release