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 3.1+
- fileinfo PHP extension
Installation via Composer
cd path/to/my/silverstripe/site composer require "silverstripe/mimevalidator:*"
The validator is not used by default. It can be enabled in a couple of ways:
Injector: Upload_Validator: class: MimeUploadValidator
NOTE: This will not work in SilverStripe 3.1.5, as there is no injector
Upload_Validator. You will need to wait until 3.1.6 or greater
is released, or use the 3.1 branch of framework in your project.
Enable on an individual upload field
$field = UploadField::create(); $field->setValidator(new MimeUploadValidator());
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:
MimeUploadValidator: MimeTypes: ics: - 'text/plain' - 'text/calendar'