# UploadedFile

> UploadedFile represents the information for an uploaded file.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › UploadedFile

| class | `yii\web\UploadedFile` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/UploadedFile.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/UploadedFile.php#L34) |

1 subclass:

- [`yii2\extensions\psrbridge\http\UploadedFile`](/docs/extensions/psr-bridge/reference/http/uploadedfile/index.md)

UploadedFile represents the information for an uploaded file.

You can call [`getInstance()`](/docs/api/yii/web/uploadedfile/index.md#method-getInstance) to retrieve the instance of an uploaded file,
and then use [`saveAs()`](/docs/api/yii/web/uploadedfile/index.md#method-saveAs) to save it on the server.
You may also query other information about the file, including [`$name`](/docs/api/yii/web/uploadedfile/index.md#property-name),
[`$tempName`](/docs/api/yii/web/uploadedfile/index.md#property-tempName), [`$type`](/docs/api/yii/web/uploadedfile/index.md#property-type), [`$size`](/docs/api/yii/web/uploadedfile/index.md#property-size), [`$error`](/docs/api/yii/web/uploadedfile/index.md#property-error) and [`$fullPath`](/docs/api/yii/web/uploadedfile/index.md#property-fullPath).

For more details and usage information on UploadedFile, see the [guide article on handling uploads](/docs/guide/input-file-upload/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-baseName"></span>`$baseName` read-only | `string` | Original file base name. | `UploadedFile` |
| <span id="property-error"></span>`$error` | `int` | An error code describing the status of this file uploading. Default: `null`. | `UploadedFile` |
| <span id="property-extension"></span>`$extension` read-only | `string` | File extension. | `UploadedFile` |
| [`$fullPath`](#property-fullPath) | `string\|null` | The full path as submitted by the browser. Default: `null`. Available since 2.0.46. | `UploadedFile` |
| [`$hasError`](#property-hasError) read-only | `bool` | Whether there is an error with the uploaded file. | `UploadedFile` |
| <span id="property-name"></span>`$name` | `string` | The original name of the file being uploaded Default: `null`. | `UploadedFile` |
| <span id="property-size"></span>`$size` | `int` | The actual size of the uploaded file in bytes Default: `null`. | `UploadedFile` |
| [`$tempName`](#property-tempName) | `string` | The path of the uploaded file on the server. Default: `null`. | `UploadedFile` |
| [`$type`](#property-type) | `string` | The MIME-type of the uploaded file (such as "image/gif"). Default: `null`. | `UploadedFile` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | UploadedFile constructor. | `UploadedFile` |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`__toString()`](#method-__toString) | String output. | `UploadedFile` |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`getBaseName()`](#method-getBaseName) |  | `UploadedFile` |
| [`getExtension()`](#method-getExtension) |  | `UploadedFile` |
| [`getHasError()`](#method-getHasError) |  | `UploadedFile` |
| [`getInstance()`](#method-getInstance) static | Returns an uploaded file for the given model attribute. | `UploadedFile` |
| [`getInstanceByName()`](#method-getInstanceByName) static | Returns an uploaded file according to the given file input name. | `UploadedFile` |
| [`getInstances()`](#method-getInstances) static | Returns all uploaded files for the given model attribute. | `UploadedFile` |
| [`getInstancesByName()`](#method-getInstancesByName) static | Returns an array of uploaded files corresponding to the specified file input name. | `UploadedFile` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`reset()`](#method-reset) static | Cleans up the loaded UploadedFile instances. | `UploadedFile` |
| [`saveAs()`](#method-saveAs) | Saves the uploaded file. | `UploadedFile` |
| [`copyTempFile()`](#method-copyTempFile) protected | Copy temporary file into file specified | `UploadedFile` |

## Property details

### $fullPath <span id="property-fullPath"></span>

```php
public string|null $fullPath = null
```

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.

**Since**

2.0.46

### $hasError <span id="property-hasError"></span>

```php
public bool $hasError
```

Whether there is an error with the uploaded file.

Check [`$error`](/docs/api/yii/web/uploadedfile/index.md#property-error) for
detailed error code information.

**Access**

Read-only (virtual property).

### $tempName <span id="property-tempName"></span>

```php
public string $tempName = null
```

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.

### $type <span id="property-type"></span>

```php
public string $type = null
```

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 [`FileHelper::getMimeType()`](/docs/api/yii/helpers/basefilehelper/index.md#method-getMimeType) to determine the exact MIME type.

## Method details

### __construct() <span id="method-__construct"></span>

```php
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() <span id="method-__toString"></span>

```php
public function __toString(): string
```

String 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() <span id="method-getBaseName"></span>

```php
public function getBaseName(): string
```

**Returns**

`string`: Original file base name

### getExtension() <span id="method-getExtension"></span>

```php
public function getExtension(): string
```

**Returns**

`string`: File extension

### getHasError() <span id="method-getHasError"></span>

```php
public function getHasError(): bool
```

**Returns**

`bool`: Whether there is an error with the uploaded file. Check [`$error`](/docs/api/yii/web/uploadedfile/index.md#property-error) for detailed error code information.

### getInstance() <span id="method-getInstance"></span>

```php
public static function getInstance(yii\base\Model $model, string $attribute): yii\web\UploadedFile|null
```

Returns an uploaded file for the given model attribute.

The file should be uploaded using [`ActiveField::fileInput()`](/docs/api/yii/widgets/activefield/index.md#method-fileInput).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md) | 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`](/docs/api/yii/web/uploadedfile/index.md)`|null`: The instance of the uploaded file. Null is returned if no file is uploaded for the specified model attribute.

**See also**

- [`getInstanceByName()`](/docs/api/yii/web/uploadedfile/index.md#method-getInstanceByName)

### getInstanceByName() <span id="method-getInstanceByName"></span>

```php
public static function getInstanceByName(string $name): yii\web\UploadedFile|null
```

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]').

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the file input field. |

**Returns**

[`UploadedFile`](/docs/api/yii/web/uploadedfile/index.md)`|null`: The instance of the uploaded file. Null is returned if no file is uploaded for the specified name.

### getInstances() <span id="method-getInstances"></span>

```php
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\base\Model`](/docs/api/yii/base/model/index.md) | 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`](/docs/api/yii/web/uploadedfile/index.md)`[]`: Array of UploadedFile objects. Empty array is returned if no available file was found for the given attribute.

### getInstancesByName() <span id="method-getInstancesByName"></span>

```php
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`](/docs/api/yii/web/uploadedfile/index.md)`[]`: 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() <span id="method-reset"></span>

```php
public static function reset(): mixed
```

Cleans up the loaded UploadedFile instances.

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

**Returns**

`mixed`

### saveAs() <span id="method-saveAs"></span>

```php
public function saveAs(string $file, bool $deleteTempFile = true): bool
```

Saves 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**

- [`$error`](/docs/api/yii/web/uploadedfile/index.md#property-error)

### copyTempFile() <span id="method-copyTempFile"></span>

```php
protected function copyTempFile(string $targetFile): int|false
```

Copy 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