Checks uploaded file content roughly matches a known MIME type for the file extension.
PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.tx
_config
lang
src
tests
.editorconfig
.gitattributes
.gitignore
.scrutinizer.yml
.travis.yml
.upgrade.yml
README.md
code-of-conduct.md
codecov.yml
composer.json
license.md
phpcs.xml.dist FIX: Update CI support and readme docs Dec 12, 2017
phpunit.xml.dist

README.md

MIME upload validator

Build Status Scrutinizer Code Quality codecov SilverStripe supported module

Introduction

Checks uploaded file content roughly matches a known MIME type for the file extension.

It can be used with FileField or any subclasses like UploadField.

For example, it will fail validation if someone renames a .exe file to .jpg and attempts to upload the file.

Requirements

Note: For a SilverStripe 3.x compatible version, please use the 1.x release line.

Installation via Composer

Install with composer by running composer require silverstripe/mimevalidator in the root of your SilverStripe project.

Configuration

The validator is not used by default. It can be enabled in a couple of ways:

Enable globally

In your mysite/_config/config.yml file:

SilverStripe\Core\Injector\Injector:
  SilverStripe\Assets\Upload_Validator:
    class: SilverStripe\MimeValidator\MimeUploadValidator

Enable on an individual upload field

$field = UploadField::create();
$field->setValidator(MimeUploadValidator::create());

Adding MIME types

By default MIME types are checked against HTTP.MimeTypes config set in framework. This can be limiting as this only allows for one MIME type per extension. To allow for multiple MIME types per extension, you can add these in your YAML config as below:

SilverStripe\MimeValidator\MimeUploadValidator:
  MimeTypes:
    ics:
      - 'text/plain'
      - 'text/calendar'