class MimeUploadValidator extends Upload_Validator (View source)

Adds an additional validation rule to Upload_Validator that attempts to detect the file extension of an uploaded file matches it's contents, which is done by detecting the MIME type and doing a fuzzy match.

Class MimeUploadValidator

Traits

A class that can be instantiated or replaced via DI

Provides extensions to this object to integrate it with standard config API methods.

Config options

default_max_file_size array

Contains a list of the max file sizes shared by all upload fields. This is then duplicated into the "allowedMaxFileSize" instance property on construct.

from  Upload_Validator
use_is_uploaded_file bool

Set to false to assume is_uploaded_file() is true, Set to true to actually call is_uploaded_file() Useful to use when testing uploads

from  Upload_Validator

Properties

protected array $tmpFile

Information about the temporary file produced by the PHP-runtime.

from  Upload_Validator
protected $errors from  Upload_Validator
public array $allowedMaxFileSize

Restrict filesize for either all filetypes or a specific extension, with extension-name as array-key and the size-restriction in bytes as array-value.

from  Upload_Validator
public array $allowedExtensions from  Upload_Validator
protected string $filterPattern

The preg_replace() pattern to use against MIME types. Used to strip out useless characters so matching of MIME types can be fuzzy.

Methods

public static 
create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

public static 
singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

public static 
config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

public
mixed
stat(string $name) deprecated

Get inherited config value

public
mixed
uninherited(string $name)

Gets the uninherited value for the given config option

public
$this
set_stat(string $name, mixed $value) deprecated

Update the config value for a given property

public
array
getErrors()

Return all errors that occurred while validating the temporary file.

public
clearErrors()

Clear out all errors

public
setTmpFile(array $tmpFile)

Set information about temporary file produced by PHP.

public
null|int
getLargestAllowedMaxFileSize()

Returns the largest maximum filesize allowed across all extensions

public
int
getAllowedMaxFileSize(string $ext = null)

Get maximum file size for all or specified file extension.

public
setAllowedMaxFileSize(array|int|string $rules)

Set filesize maximums (in bytes or INI format).

public
array
getAllowedExtensions()

No description

public
setAllowedExtensions(array $rules)

Limit allowed file extensions. Empty by default, allowing all extensions.

public
bool
isValidSize()

Determines if the bytesize of an uploaded file is valid - can be defined on an extension-by-extension basis in $allowedMaxFileSize

public
bool
isFileEmpty()

Determine if this file is valid but empty

public
bool
isValidExtension()

Determines if the temporary file has a valid extension An empty string in the validation map indicates files without an extension.

public
string
getFileExtension()

Return the extension of the uploaded file, in lowercase Returns an empty string for files without an extension

public
bool
validate()

Run through the rules for this validator checking against the temporary file set by setTmpFile() to see if the file is deemed valid or not.

public
bool
isValidUpload()

Check that a valid file was given for upload (ignores file size)

public
bool
isCompleteUpload()

Check whether the file was fully uploaded

public
setFilterPattern(string $pattern)

No description

public
string
getFilterPattern()

No description

public
bool|null
isValidMime()

Check if the temporary file has a valid MIME type for it's extension.

public
array
getExpectedMimeTypes($file)

Fetches an array of valid mimetypes.

public
bool
compareMime(string $first, string $second)

Check two MIME types roughly match eachother.

Details

static Injectable create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.

This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();

Parameters

mixed ...$args

Return Value

Injectable

static Injectable singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).

Parameters

string $class

Optional classname to create, if the called class should not be used

Return Value

Injectable

The singleton instance

static Config_ForClass config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

Return Value

Config_ForClass

mixed stat(string $name) deprecated

deprecated 5.0 Use ->config()->get() instead

Get inherited config value

Parameters

string $name

Return Value

mixed

mixed uninherited(string $name)

Gets the uninherited value for the given config option

Parameters

string $name

Return Value

mixed

$this set_stat(string $name, mixed $value) deprecated

deprecated 5.0 Use ->config()->set() instead

Update the config value for a given property

Parameters

string $name
mixed $value

Return Value

$this

array getErrors()

Return all errors that occurred while validating the temporary file.

Return Value

array

clearErrors()

Clear out all errors

setTmpFile(array $tmpFile)

Set information about temporary file produced by PHP.

Parameters

array $tmpFile

null|int getLargestAllowedMaxFileSize()

Returns the largest maximum filesize allowed across all extensions

Return Value

null|int

Filesize in bytes

int getAllowedMaxFileSize(string $ext = null)

Get maximum file size for all or specified file extension.

Parameters

string $ext

Return Value

int

Filesize in bytes

setAllowedMaxFileSize(array|int|string $rules)

Set filesize maximums (in bytes or INI format).

Automatically converts extensions to lowercase for easier matching.

Example:

array('*' => 200, 'jpg' => 1000, '[doc]' => '5m')

Parameters

array|int|string $rules

array getAllowedExtensions()

No description

Return Value

array

setAllowedExtensions(array $rules)

Limit allowed file extensions. Empty by default, allowing all extensions.

To allow files without an extension, use an empty string. See File::$allowed_extensions to get a good standard set of extensions that are typically not harmful in a webserver context. See setAllowedMaxFileSize() to limit file size by extension.

Parameters

array $rules

List of extensions

bool isValidSize()

Determines if the bytesize of an uploaded file is valid - can be defined on an extension-by-extension basis in $allowedMaxFileSize

Return Value

bool

bool isFileEmpty()

Determine if this file is valid but empty

Return Value

bool

bool isValidExtension()

Determines if the temporary file has a valid extension An empty string in the validation map indicates files without an extension.

Return Value

bool

string getFileExtension()

Return the extension of the uploaded file, in lowercase Returns an empty string for files without an extension

Return Value

string

bool validate()

Run through the rules for this validator checking against the temporary file set by setTmpFile() to see if the file is deemed valid or not.

Return Value

bool

bool isValidUpload()

Check that a valid file was given for upload (ignores file size)

Return Value

bool

bool isCompleteUpload()

Check whether the file was fully uploaded

Return Value

bool

setFilterPattern(string $pattern)

No description

Parameters

string $pattern

string getFilterPattern()

No description

Return Value

string

bool|null isValidMime()

Check if the temporary file has a valid MIME type for it's extension.

Return Value

bool|null

Exceptions

MimeUploadValidatorException

array getExpectedMimeTypes($file)

Fetches an array of valid mimetypes.

Parameters

$file

Return Value

array

Exceptions

MimeUploadValidatorException

bool compareMime(string $first, string $second)

Check two MIME types roughly match eachother.

Before we check MIME types, remove known prefixes "vnd.", "x-" etc. If there is a suffix, we'll use that to compare. Examples:

application/x-json = json application/json = json application/xhtml+xml = xml application/xml = xml

Parameters

string $first

The first MIME type to compare to the second

string $second

The second MIME type to compare to the first

Return Value

bool