Class yii\web\UploadedFile

Inheritanceyii\web\UploadedFile » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/web/UploadedFile.php

UploadedFile represents the information for an uploaded file.

You can call getInstance() to retrieve the instance of an uploaded file, and then use saveAs() to save it on the server. You may also query other information about the file, including $name, $tempName, $type, $size, $error and $fullPath.

For more details and usage information on UploadedFile, see the guide article on handling uploads.

Public Properties

Hide inherited properties

Property Type Description Defined By
$baseName string Original file base name. yii\web\UploadedFile
$error integer An error code describing the status of this file uploading. yii\web\UploadedFile
$extension string File extension. yii\web\UploadedFile
$fullPath string|null The full path as submitted by the browser. yii\web\UploadedFile
$hasError boolean Whether there is an error with the uploaded file. yii\web\UploadedFile
$name string The original name of the file being uploaded yii\web\UploadedFile
$size integer The actual size of the uploaded file in bytes yii\web\UploadedFile
$tempName string The path of the uploaded file on the server. yii\web\UploadedFile
$type string The MIME-type of the uploaded file (such as "image/gif"). yii\web\UploadedFile

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() UploadedFile constructor. yii\web\UploadedFile
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__toString() String output. yii\web\UploadedFile
__unset() Sets an object property to null. yii\base\BaseObject
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
getBaseName() yii\web\UploadedFile
getExtension() yii\web\UploadedFile
getHasError() yii\web\UploadedFile
getInstance() Returns an uploaded file for the given model attribute. yii\web\UploadedFile
getInstanceByName() Returns an uploaded file according to the given file input name. yii\web\UploadedFile
getInstances() Returns all uploaded files for the given model attribute. yii\web\UploadedFile
getInstancesByName() Returns an array of uploaded files corresponding to the specified file input name. yii\web\UploadedFile
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
reset() Cleans up the loaded UploadedFile instances. yii\web\UploadedFile
saveAs() Saves the uploaded file. yii\web\UploadedFile

Protected Methods

Hide inherited methods

Method Description Defined By
copyTempFile() Copy temporary file into file specified yii\web\UploadedFile

Property Details

Hide inherited properties

$baseName public property

Original file base name.

public string $baseName null
$error public property

An error code describing the status of this file uploading.

See also https://www.php.net/manual/en/features.file-upload.errors.php.

public integer $error null
$extension public property

File extension.

public string $extension null
$fullPath public property (available since version 2.0.46)

The full path as submitted by the browser. Note this value does not always contain a real directory structure, and cannot be trusted. Available as of PHP 8.1.

public string|null $fullPath null
$hasError public property

Whether there is an error with the uploaded file. Check $error for detailed error code information.

public boolean $hasError null
$name public property

The original name of the file being uploaded

public string $name null
$size public property

The actual size of the uploaded file in bytes

public integer $size null
$tempName public property

The path of the uploaded file on the server. Note, this is a temporary file which will be automatically deleted by PHP after the current request is processed.

public string $tempName null
$type public property

The MIME-type of the uploaded file (such as "image/gif"). Since this MIME type is not checked on the server-side, do not take this value for granted. Instead, use yii\helpers\FileHelper::getMimeType() to determine the exact MIME type.

public string $type null

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

UploadedFile constructor.

public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    $this->_tempResource = ArrayHelper::remove($config, 'tempResource');
    parent::__construct($config);
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__toString() public method

String output.

This is PHP magic method that returns string representation of an object. The implementation here returns the uploaded file's name.

public string __toString ( )
return string

The string representation of the object

                public function __toString()
{
    return $this->name;
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
copyTempFile() protected method (available since version 2.0.32)

Copy temporary file into file specified

protected integer|false copyTempFile ( $targetFile )
$targetFile string

Path of the file to copy to

return integer|false

The total count of bytes copied, or false on failure

                protected function copyTempFile($targetFile)
{
    $target = fopen($targetFile, 'wb');
    if ($target === false) {
        return false;
    }
    $result = stream_copy_to_stream($this->_tempResource, $target);
    @fclose($target);
    return $result;
}

            
getBaseName() public method

public string getBaseName ( )
return string

Original file base name

                public function getBaseName()
{
    // https://github.com/yiisoft/yii2/issues/11012
    $pathInfo = pathinfo('_' . $this->name, PATHINFO_FILENAME);
    return mb_substr($pathInfo, 1, mb_strlen($pathInfo, '8bit'), '8bit');
}

            
getExtension() public method

public string getExtension ( )
return string

File extension

                public function getExtension()
{
    return strtolower(pathinfo($this->name, PATHINFO_EXTENSION));
}

            
getHasError() public method

public boolean getHasError ( )
return boolean

Whether there is an error with the uploaded file. Check $error for detailed error code information.

                public function getHasError()
{
    return $this->error != UPLOAD_ERR_OK;
}

            
getInstance() public static method

Returns an uploaded file for the given model attribute.

The file should be uploaded using yii\widgets\ActiveField::fileInput().

See also getInstanceByName().

public static yii\web\UploadedFile|null getInstance ( $model, $attribute )
$model yii\base\Model

The data model

$attribute string

The attribute name. The attribute name may contain array indexes. For example, '[1]file' for tabular file uploading; and 'file[1]' for an element in a file array.

return yii\web\UploadedFile|null

The instance of the uploaded file. Null is returned if no file is uploaded for the specified model attribute.

                public static function getInstance($model, $attribute)
{
    $name = Html::getInputName($model, $attribute);
    return static::getInstanceByName($name);
}

            
getInstanceByName() public static method

Returns an uploaded file according to the given file input name.

The name can be a plain string or a string like an array element (e.g. 'Post[imageFile]', or 'Post[0][imageFile]').

public static yii\web\UploadedFile|null getInstanceByName ( $name )
$name string

The name of the file input field.

return yii\web\UploadedFile|null

The instance of the uploaded file. Null is returned if no file is uploaded for the specified name.

                public static function getInstanceByName($name)
{
    $files = self::loadFiles();
    return isset($files[$name]) ? new static($files[$name]) : null;
}

            
getInstances() public static method

Returns all uploaded files for the given model attribute.

public static yii\web\UploadedFile[] getInstances ( $model, $attribute )
$model yii\base\Model

The data model

$attribute string

The attribute name. The attribute name may contain array indexes for tabular file uploading, e.g. '[1]file'.

return yii\web\UploadedFile[]

Array of UploadedFile objects. Empty array is returned if no available file was found for the given attribute.

                public static function getInstances($model, $attribute)
{
    $name = Html::getInputName($model, $attribute);
    return static::getInstancesByName($name);
}

            
getInstancesByName() public static method

Returns an array of uploaded files corresponding to the specified file input name.

This is mainly used when multiple files were uploaded and saved as 'files[0]', 'files[1]', 'files[n]'..., and you can retrieve them all by passing 'files' as the name.

public static yii\web\UploadedFile[] getInstancesByName ( $name )
$name string

The name of the array of files

return yii\web\UploadedFile[]

The array of UploadedFile objects. Empty array is returned if no adequate upload was found. Please note that this array will contain all files from all sub-arrays regardless how deeply nested they are.

                public static function getInstancesByName($name)
{
    $files = self::loadFiles();
    if (isset($files[$name])) {
        return [new static($files[$name])];
    }
    $results = [];
    foreach ($files as $key => $file) {
        if (strpos($key, "{$name}[") === 0) {
            $results[] = new static($file);
        }
    }
    return $results;
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}

            
reset() public static method

Cleans up the loaded UploadedFile instances.

This method is mainly used by test scripts to set up a fixture.

public static void reset ( )

                public static function reset()
{
    self::$_files = null;
}

            
saveAs() public method

Saves the uploaded file.

If the target file $file already exists, it will be overwritten.

See also $error.

public boolean saveAs ( $file, $deleteTempFile true )
$file string

The file path or a path alias used to save the uploaded file.

$deleteTempFile boolean

Whether to delete the temporary file after saving. If true, you will not be able to save the uploaded file again in the current request.

return boolean

True whether the file is saved successfully

                public function saveAs($file, $deleteTempFile = true)
{
    if ($this->hasError) {
        return false;
    }
    $targetFile = Yii::getAlias($file);
    if (is_resource($this->_tempResource)) {
        $result = $this->copyTempFile($targetFile);
        return $deleteTempFile ? @fclose($this->_tempResource) : (bool) $result;
    }
    return $deleteTempFile ? move_uploaded_file($this->tempName, $targetFile) : copy($this->tempName, $targetFile);
}