PHP Class yii\validators\FileValidator

Note that you should enable fileinfo PHP extension.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends Validator
Mostra file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$checkExtensionByMimeType whether to check file type (extension) with mime-type. If extension produced by file mime-type check differs from uploaded file extension, the file will be considered as invalid.
$extensions a list of file name extensions that are allowed to be uploaded. This can be either an array or a string consisting of file extension names separated by space or comma (e.g. "gif, jpg"). Extension names are case-insensitive. Defaults to null, meaning all file name extensions are allowed.
$maxFiles the maximum file count the given attribute can hold. Defaults to 1, meaning single file upload. By defining a higher number, multiple uploads become possible. Setting it to 0 means there is no limit on the number of files that can be uploaded simultaneously. > Note: The maximum number of files allowed to be uploaded simultaneously is also limited with PHP directive max_file_uploads, which defaults to 20.
$maxSize the maximum number of bytes required for the uploaded file. Defaults to null, meaning no limit. Note, the size limit is also affected by upload_max_filesize and post_max_size INI setting and the 'MAX_FILE_SIZE' hidden field value. See FileValidator::getSizeLimit for details.
$message the error message used when a file is not uploaded correctly.
$mimeTypes a list of file MIME types that are allowed to be uploaded. This can be either an array or a string consisting of file MIME types separated by space or comma (e.g. "text/plain, image/png"). The mask with the special character * can be used to match groups of mime types. For example image/* will pass all mime types, that begin with image/ (e.g. image/jpeg, image/png). Mime type names are case-insensitive. Defaults to null, meaning all MIME types are allowed.
$minSize the minimum number of bytes required for the uploaded file. Defaults to null, meaning no limit.
$tooBig the error message used when the uploaded file is too large. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {limit}: the maximum size allowed (see FileValidator::getSizeLimit) - {formattedLimit}: the maximum size formatted with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]]
$tooMany the error message used if the count of multiple uploads exceeds limit. You may use the following tokens in the message: - {attribute}: the attribute name - {limit}: the value of [[maxFiles]]
$tooSmall the error message used when the uploaded file is too small. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {limit}: the value of [[minSize]] - {formattedLimit}: the value of [[minSize]] formatted with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]
$uploadRequired the error message used when no file is uploaded. Note that this is the text of the validation error message. To make uploading files required, you have to set [[skipOnEmpty]] to false.
$wrongExtension the error message used when the uploaded file has an extension name that is not listed in [[extensions]]. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {extensions}: the list of the allowed extensions.
$wrongMimeType the error message used when the file has an mime type that is not allowed by [[mimeTypes]] property. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {mimeTypes}: the value of [[mimeTypes]]

Public Methods

Method Description
clientValidateAttribute ( $model, $attribute, $view )
getSizeLimit ( ) : integer Returns the maximum size allowed for uploaded files.
init ( )
isEmpty ( $value, boolean $trim = false )
validateAttribute ( $model, $attribute )

Protected Methods

Method Description
getClientOptions ( Model $model, string $attribute ) : array Returns the client-side validation options.
validateExtension ( UploadedFile $file ) : boolean Checks if given uploaded file have correct type (extension) according current validator settings.
validateMimeType ( UploadedFile $file ) : boolean Checks the mimeType of the $file against the list in the [[mimeTypes]] property
validateValue ( $value )

Private Methods

Method Description
buildMimeTypeRegexp ( string $mask ) : string Builds the RegExp from the $mask
sizeToBytes ( string $sizeStr ) : integer Converts php.ini style size to bytes

Method Details

clientValidateAttribute() public method

public clientValidateAttribute ( $model, $attribute, $view )

getClientOptions() protected method

Returns the client-side validation options.
protected getClientOptions ( Model $model, string $attribute ) : array
$model yii\base\Model the model being validated
$attribute string the attribute name being validated
return array the client-side validation options

getSizeLimit() public method

This is determined based on four factors: - 'upload_max_filesize' in php.ini - 'post_max_size' in php.ini - 'MAX_FILE_SIZE' hidden field - [[maxSize]]
public getSizeLimit ( ) : integer
return integer the size limit for uploaded files.

init() public method

public init ( )

isEmpty() public method

public isEmpty ( $value, boolean $trim = false )
$trim boolean

validateAttribute() public method

public validateAttribute ( $model, $attribute )

validateExtension() protected method

Checks if given uploaded file have correct type (extension) according current validator settings.
protected validateExtension ( UploadedFile $file ) : boolean
$file yii\web\UploadedFile
return boolean

validateMimeType() protected method

Checks the mimeType of the $file against the list in the [[mimeTypes]] property
See also: mimeTypes
Since: 2.0.8
protected validateMimeType ( UploadedFile $file ) : boolean
$file yii\web\UploadedFile
return boolean whether the $file mimeType is allowed

validateValue() protected method

protected validateValue ( $value )

Property Details

$checkExtensionByMimeType public_oe property

whether to check file type (extension) with mime-type. If extension produced by file mime-type check differs from uploaded file extension, the file will be considered as invalid.
public $checkExtensionByMimeType

$extensions public_oe property

a list of file name extensions that are allowed to be uploaded. This can be either an array or a string consisting of file extension names separated by space or comma (e.g. "gif, jpg"). Extension names are case-insensitive. Defaults to null, meaning all file name extensions are allowed.
See also: wrongExtension for the customized message for wrong file type.
public $extensions

$maxFiles public_oe property

the maximum file count the given attribute can hold. Defaults to 1, meaning single file upload. By defining a higher number, multiple uploads become possible. Setting it to 0 means there is no limit on the number of files that can be uploaded simultaneously. > Note: The maximum number of files allowed to be uploaded simultaneously is also limited with PHP directive max_file_uploads, which defaults to 20.
See also: http://php.net/manual/en/ini.core.php#ini.max-file-uploads
See also: tooMany for the customized message when too many files are uploaded.
public $maxFiles

$maxSize public_oe property

the maximum number of bytes required for the uploaded file. Defaults to null, meaning no limit. Note, the size limit is also affected by upload_max_filesize and post_max_size INI setting and the 'MAX_FILE_SIZE' hidden field value. See FileValidator::getSizeLimit for details.
See also: http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
See also: http://php.net/post-max-size
See also: getSizeLimit
See also: tooBig for the customized message for a file that is too big.
public $maxSize

$message public_oe property

the error message used when a file is not uploaded correctly.
public $message

$mimeTypes public_oe property

a list of file MIME types that are allowed to be uploaded. This can be either an array or a string consisting of file MIME types separated by space or comma (e.g. "text/plain, image/png"). The mask with the special character * can be used to match groups of mime types. For example image/* will pass all mime types, that begin with image/ (e.g. image/jpeg, image/png). Mime type names are case-insensitive. Defaults to null, meaning all MIME types are allowed.
See also: wrongMimeType for the customized message for wrong MIME type.
public $mimeTypes

$minSize public_oe property

the minimum number of bytes required for the uploaded file. Defaults to null, meaning no limit.
See also: tooSmall for the customized message for a file that is too small.
public $minSize

$tooBig public_oe property

the error message used when the uploaded file is too large. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {limit}: the maximum size allowed (see FileValidator::getSizeLimit) - {formattedLimit}: the maximum size formatted with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]]
public $tooBig

$tooMany public_oe property

the error message used if the count of multiple uploads exceeds limit. You may use the following tokens in the message: - {attribute}: the attribute name - {limit}: the value of [[maxFiles]]
public $tooMany

$tooSmall public_oe property

the error message used when the uploaded file is too small. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {limit}: the value of [[minSize]] - {formattedLimit}: the value of [[minSize]] formatted with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]
public $tooSmall

$uploadRequired public_oe property

the error message used when no file is uploaded. Note that this is the text of the validation error message. To make uploading files required, you have to set [[skipOnEmpty]] to false.
public $uploadRequired

$wrongExtension public_oe property

the error message used when the uploaded file has an extension name that is not listed in [[extensions]]. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {extensions}: the list of the allowed extensions.
public $wrongExtension

$wrongMimeType public_oe property

the error message used when the file has an mime type that is not allowed by [[mimeTypes]] property. You may use the following tokens in the message: - {attribute}: the attribute name - {file}: the uploaded file name - {mimeTypes}: the value of [[mimeTypes]]
public $wrongMimeType