MIME upload validator
Checks uploaded file content roughly matches a known MIME type for the file extension.
It can be used with
FileField or any subclasses like
For example, it will fail validation if someone renames a
.exe file to
and attempts to upload the file.
- SilverStripe 4.0+
- fileinfo PHP extension
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.
The validator is not used by default. It can be enabled in a couple of ways:
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'