UploadedFile
API reference › yii › web › UploadedFile
1 subclass
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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$baseName read-only | string | Original file base name. | UploadedFile |
$error | int | An error code describing the status of this file uploading. Default: null. | UploadedFile |
$extension read-only | string | File extension. | UploadedFile |
$fullPath | string| | The full path as submitted by the browser. Default: null. Available since 2.0.46. | UploadedFile |
$hasError read-only | bool | Whether there is an error with the uploaded file. | UploadedFile |
$name | string | The original name of the file being uploaded Default: null. | UploadedFile |
$size | int | The actual size of the uploaded file in bytes Default: null. | UploadedFile |
$tempName | string | The path of the uploaded file on the server. Default: null. | UploadedFile |
$type | string | The MIME-type of the uploaded file (such as “image/gif”). Default: null. | UploadedFile |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | UploadedFile constructor. | UploadedFile |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__toString() | String output. | UploadedFile |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
getBaseName() | UploadedFile | |
getExtension() | UploadedFile | |
getHasError() | UploadedFile | |
getInstance() static | Returns an uploaded file for the given model attribute. | UploadedFile |
getInstanceByName() static | Returns an uploaded file according to the given file input name. | UploadedFile |
getInstances() static | Returns all uploaded files for the given model attribute. | UploadedFile |
getInstancesByName() static | Returns an array of uploaded files corresponding to the specified file input name. | UploadedFile |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
reset() static | Cleans up the loaded UploadedFile instances. | UploadedFile |
saveAs() | Saves the uploaded file. | UploadedFile |
copyTempFile() protected | Copy temporary file into file specified | UploadedFile |
Property details
Section titled “Property details”$fullPath
Section titled “$fullPath ”public string|null $fullPath = nullThe 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.
Since
2.0.46
$hasError
Section titled “$hasError ”public bool $hasErrorWhether there is an error with the uploaded file.
Check $error for
detailed error code information.
Access
Read-only (virtual property).
$tempName
Section titled “$tempName ”public string $tempName = nullThe 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.
$type
Section titled “$type ”public string $type = nullThe 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 FileHelper::getMimeType() to determine the exact MIME type.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(array $config = [])UploadedFile constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | Name-value pairs that will be used to initialize the object properties |
__toString()
Section titled “__toString() ”public function __toString(): stringString output.
This is PHP magic method that returns string representation of an object. The implementation here returns the uploaded file’s name.
Returns
string: The string representation of the object
getBaseName()
Section titled “getBaseName() ”public function getBaseName(): stringReturns
string: Original file base name
getExtension()
Section titled “getExtension() ”public function getExtension(): stringReturns
string: File extension
getHasError()
Section titled “getHasError() ”public function getHasError(): boolReturns
bool: Whether there is an error with the uploaded file. Check $error for detailed error code information.
getInstance()
Section titled “getInstance() ”public static function getInstance(yii\base\Model $model, string $attribute): yii\web\UploadedFile|nullReturns an uploaded file for the given model attribute.
The file should be uploaded using ActiveField::fileInput().
Parameters
| Parameter | Type | Description |
|---|---|---|
$model | yii\ | 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. |
Returns
UploadedFile|null: The instance of the uploaded file. Null is returned if no file is uploaded for the specified model attribute.
See also
getInstanceByName()
Section titled “getInstanceByName() ”public static function getInstanceByName(string $name): yii\web\UploadedFile|nullReturns 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]’).
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the file input field. |
Returns
UploadedFile|null: The instance of the uploaded file. Null is returned if no file is uploaded for the specified name.
getInstances()
Section titled “getInstances() ”public static function getInstances(yii\base\Model $model, string $attribute): yii\web\UploadedFile[]Returns all uploaded files for the given model attribute.
Parameters
| Parameter | Type | Description |
|---|---|---|
$model | yii\ | The data model |
$attribute | string | The attribute name. The attribute name may contain array indexes for tabular file uploading, e.g. ‘[1]file’. |
Returns
UploadedFile[]: Array of UploadedFile objects. Empty array is returned if no available file was found for the given attribute.
getInstancesByName()
Section titled “getInstancesByName() ”public static function getInstancesByName(string $name): yii\web\UploadedFile[]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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the array of files |
Returns
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()
Section titled “reset() ”public static function reset(): mixedCleans up the loaded UploadedFile instances.
This method is mainly used by test scripts to set up a fixture.
Returns
mixed
saveAs()
Section titled “saveAs() ”public function saveAs(string $file, bool $deleteTempFile = true): boolSaves the uploaded file.
If the target file $file already exists, it will be overwritten.
Parameters
| Parameter | Type | Description |
|---|---|---|
$file | string | The file path or a path alias used to save the uploaded file. |
$deleteTempFile | bool | 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. |
Returns
bool: True whether the file is saved successfully
See also
copyTempFile()
Section titled “copyTempFile() ”protected function copyTempFile(string $targetFile): int|falseCopy temporary file into file specified
Parameters
| Parameter | Type | Description |
|---|---|---|
$targetFile | string | Path of the file to copy to |
Returns
int|false: The total count of bytes copied, or false on failure
Since
2.0.32