Class CFileValidator
CFileValidator verifies if an attribute is receiving a valid uploaded file.
It uses the model class and attribute name to retrieve the information about the uploaded file. It then checks if a file is uploaded successfully, if the file size is within the limit and if the file type is allowed.
This validator will attempt to fetch uploaded data if attribute is not previously set. Please note that this cannot be done if input is tabular:
foreach($models as $i=>$model) $model->attribute = CUploadedFile::getInstance($model, "[$i]attribute");
Please note that you must use CUploadedFile::getInstances()
for
multiple file uploads.
When using CFileValidator with an active record, the following code is often used:
$model->attribute = CUploadedFile::getInstance($model, "attribute"); if($model->save()) { // single upload $model->attribute->saveAs($path); // multiple upload foreach($model->attribute as $file) $file->saveAs($path); }
You can use CFileValidator
to validate the file attribute.
CValidator::$message
property for setting a custom error message,
CFileValidator has a few custom error messages you can set that correspond to
different validation scenarios. When the file is too large, you may use the
CFileValidator::$tooLarge
property to define a custom error message. Similarly for CFileValidator::$tooSmall
, CFileValidator::$wrongType
and CFileValidator::$tooMany
. The messages may contain
additional placeholders that will be replaced with the actual content. In
addition to the "{attribute}" placeholder, recognized by all validators (see
CValidator
), CFileValidator allows for the following placeholders to be
specified:
- {file}: replaced with the name of the file.
- {limit}: when using
CFileValidator::$tooLarge
, replaced withCFileValidator::$maxSize
; when usingCFileValidator::$tooSmall
, replaced withCFileValidator::$minSize
; and when usingCFileValidator::$tooMany
replaced withCFileValidator::$maxFiles
. - {extensions}: when using
CFileValidator::$wrongType
, it will be replaced with the allowed extensions.
- CComponent
- CValidator
- CFileValidator
Copyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.0
Located at x2engine/framework/validators/CFileValidator.php
protected
|
#
validateAttribute(
Set the attribute and then validates using |
protected
|
#
validateFile(
Internally validates a file object. |
protected
|
#
emptyAttribute(
Raises an error to inform end user about blank attribute. Sets the owner attribute to null to prevent setting arbitrary values. |
protected
integer
|
#
getSizeLimit( )
Returns the maximum size allowed for uploaded files. This is determined based on
three factors:
|
public
integer
|
#
sizeToBytes( string $sizeStr )
Converts php.ini style size to bytes. Examples of size strings are: 150, 1g, 500k, 5M (size suffix is case insensitive). If you pass here the number with a fractional part, then everything after the decimal point will be ignored (php.ini values common behavior). For example 1.5G value would be treated as 1G and 1073741824 number will be returned as a result. This method is public (was private before) since 1.1.11. |
addError(),
applyTo(),
clientValidateAttribute(),
createValidator(),
isEmpty(),
validate()
|
public
boolean
|
$allowEmpty | false |
#
whether the attribute requires a file to be uploaded or not. Defaults to false, meaning a file is required to be uploaded. When no file is uploaded, the owner attribute is set to null to prevent setting arbitrary values. |
public
mixed
|
$types |
|
#
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. |
public
mixed
|
$mimeTypes |
|
#
a list of MIME-types of the file that are allowed to be uploaded. This can be either an array or a string consisting of MIME-types separated by space or comma (e.g. "image/gif, image/jpeg"). MIME-types are case-insensitive. Defaults to null, meaning all MIME-types are allowed. In order to use this property fileinfo PECL extension should be installed. |
public
integer
|
$minSize |
|
#
the minimum number of bytes required for the uploaded file. Defaults to null, meaning no limit. |
public
integer
|
$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' INI setting and the 'MAX_FILE_SIZE' hidden field value. |
public
string
|
$tooLarge |
|
#
the error message used when the uploaded file is too large. |
public
string
|
$tooSmall |
|
#
the error message used when the uploaded file is too small. |
public
string
|
$wrongType |
|
#
the error message used when the uploaded file has an extension name that is
not listed among |
public
string
|
$wrongMimeType |
|
#
the error message used when the uploaded file has a MIME-type that is not
listed among |
public
integer
|
$maxFiles | 1 |
#
the maximum file count the given attribute can hold. It defaults to 1, meaning single file upload. By defining a higher number, multiple uploads become possible. |
public
string
|
$tooMany |
|
#
the error message used if the count of multiple uploads exceeds limit. |
$attributes,
$builtInValidators,
$enableClientValidation,
$except,
$message,
$on,
$safe,
$skipOnError
|