Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • CBooleanValidator
  • CCaptchaValidator
  • CCompareValidator
  • CDateValidator
  • CDefaultValueValidator
  • CEmailValidator
  • CExistValidator
  • CFileValidator
  • CFilterValidator
  • CInlineValidator
  • CNumberValidator
  • CRangeValidator
  • CRegularExpressionValidator
  • CRequiredValidator
  • CSafeValidator
  • CStringValidator
  • CTypeValidator
  • CUniqueValidator
  • CUnsafeValidator
  • CUrlValidator
  • CValidator
  • X2UrlValidator
  • Overview
  • Package
  • Class
  • Tree

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.

In addition to the 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 with CFileValidator::$maxSize; when using CFileValidator::$tooSmall, replaced with CFileValidator::$minSize; and when using CFileValidator::$tooMany replaced with CFileValidator::$maxFiles.
  • {extensions}: when using CFileValidator::$wrongType, it will be replaced with the allowed extensions.
CComponent
Extended by CValidator
Extended by CFileValidator
Package: system\validators
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
Methods summary
protected
# validateAttribute( CModel $object, string $attribute )

Set the attribute and then validates using CFileValidator::validateFile(). If there is any error, the error message is added to the object.

Set the attribute and then validates using CFileValidator::validateFile(). If there is any error, the error message is added to the object.

Parameters

$object
CModel
$object the object being validated
$attribute
string
$attribute the attribute being validated
protected
# validateFile( CModel $object, string $attribute, CUploadedFile $file )

Internally validates a file object.

Internally validates a file object.

Parameters

$object
CModel
$object the object being validated
$attribute
string
$attribute the attribute being validated
$file
CUploadedFile
$file uploaded file passed to check against a set of rules

Throws

CException
if failed to upload the file
protected
# emptyAttribute( CModel $object, string $attribute )

Raises an error to inform end user about blank attribute. Sets the owner attribute to null to prevent setting arbitrary values.

Raises an error to inform end user about blank attribute. Sets the owner attribute to null to prevent setting arbitrary values.

Parameters

$object
CModel
$object the object being validated
$attribute
string
$attribute the attribute being validated
protected integer
# getSizeLimit( )
Returns the maximum size allowed for uploaded files. This is determined based on three factors:
  • 'upload_max_filesize' in php.ini
  • 'MAX_FILE_SIZE' hidden field
  • CFileValidator::$maxSize
Returns the maximum size allowed for uploaded files. This is determined based on three factors:
  • 'upload_max_filesize' in php.ini
  • 'MAX_FILE_SIZE' hidden field
  • CFileValidator::$maxSize

Returns

integer
the size limit for uploaded files.
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.

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.

Parameters

$sizeStr
string
$sizeStr the size string to convert.

Returns

integer
the byte count in the given size string.

Since

1.1.11
Methods inherited from CValidator
addError(), applyTo(), clientValidateAttribute(), createValidator(), isEmpty(), validate()
Methods inherited from CComponent
__call(), __get(), __isset(), __set(), __unset(), asa(), attachBehavior(), attachBehaviors(), attachEventHandler(), canGetProperty(), canSetProperty(), detachBehavior(), detachBehaviors(), detachEventHandler(), disableBehavior(), disableBehaviors(), enableBehavior(), enableBehaviors(), evaluateExpression(), getEventHandlers(), hasEvent(), hasEventHandler(), hasProperty(), raiseEvent()
Properties summary
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.

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.

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.

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.

Since

1.1.11
public integer $minSize
#

the minimum number of bytes required for the uploaded file. Defaults to null, meaning no limit.

the minimum number of bytes required for the uploaded file. Defaults to null, meaning no limit.

See

CFileValidator::$tooSmall
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.

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.

See

CFileValidator::$tooLarge
public string $tooLarge
#

the error message used when the uploaded file is too large.

the error message used when the uploaded file is too large.

See

CFileValidator::$maxSize
public string $tooSmall
#

the error message used when the uploaded file is too small.

the error message used when the uploaded file is too small.

See

CFileValidator::$minSize
public string $wrongType
#

the error message used when the uploaded file has an extension name that is not listed among CFileValidator::$types.

the error message used when the uploaded file has an extension name that is not listed among CFileValidator::$types.

public string $wrongMimeType
#

the error message used when the uploaded file has a MIME-type that is not listed among CFileValidator::$mimeTypes. In order to use this property fileinfo PECL extension should be installed.

the error message used when the uploaded file has a MIME-type that is not listed among CFileValidator::$mimeTypes. In order to use this property fileinfo PECL extension should be installed.

Since

1.1.11
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.

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.

the error message used if the count of multiple uploads exceeds limit.

Properties inherited from CValidator
$attributes, $builtInValidators, $enableClientValidation, $except, $message, $on, $safe, $skipOnError
API documentation generated by ApiGen 2.8.0