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 and $error.

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

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$error integer An error code describing the status of this file uploading. 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

MethodDescriptionDefined 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

MethodDescriptionDefined By
copyTempFile() Copy temporary file into file specified yii\web\UploadedFile

Property Details

$error public property

An error code describing the status of this file uploading.

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

public integer $error 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

__construct() public method

UploadedFile constructor.

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

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

__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

copyTempFile() protected method (available since version 2.0.32)

Copy temporary file into file specified

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

Path of the file to copy to

return boolean|integer

The total count of bytes copied, or false on failure

getBaseName() public method

public string getBaseName ( )
return string

Original file base name

getExtension() public method

public string getExtension ( )
return string

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

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 null|yii\web\UploadedFile 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 null|yii\web\UploadedFile

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

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 null|yii\web\UploadedFile getInstanceByName ( $name )
$name string

The name of the file input field.

return null|yii\web\UploadedFile

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

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.

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.

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 ( )
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