Skip to content
PHP Ecosystem
Explore

UploadedFile

API reference › yii › web › UploadedFile

classyii\web\UploadedFile
Extendsyii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/web/UploadedFile.php
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.

PropertyTypeDescriptionDefined by
$baseName read-onlystringOriginal file base name.UploadedFile
$errorintAn error code describing the status of this file uploading. Default: null.UploadedFile
$extension read-onlystringFile extension.UploadedFile
$fullPathstring|nullThe full path as submitted by the browser. Default: null. Available since 2.0.46.UploadedFile
$hasError read-onlyboolWhether there is an error with the uploaded file.UploadedFile
$namestringThe original name of the file being uploaded Default: null.UploadedFile
$sizeintThe actual size of the uploaded file in bytes Default: null.UploadedFile
$tempNamestringThe path of the uploaded file on the server. Default: null.UploadedFile
$typestringThe MIME-type of the uploaded file (such as “image/gif”). Default: null.UploadedFile
MethodDescriptionDefined by
__construct()UploadedFile constructor.UploadedFile
__call()Calls the named method which is not a class method.yii\base\BaseObject
__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.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() staticReturns the fully qualified name of this class.yii\base\BaseObject
getBaseName()UploadedFile
getExtension()UploadedFile
getHasError()UploadedFile
getInstance() staticReturns an uploaded file for the given model attribute.UploadedFile
getInstanceByName() staticReturns an uploaded file according to the given file input name.UploadedFile
getInstances() staticReturns all uploaded files for the given model attribute.UploadedFile
getInstancesByName() staticReturns an array of uploaded files corresponding to the specified file input name.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() staticCleans up the loaded UploadedFile instances.UploadedFile
saveAs()Saves the uploaded file.UploadedFile
copyTempFile() protectedCopy temporary file into file specifiedUploadedFile
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

public bool $hasError

Whether there is an error with the uploaded file.

Check $error for detailed error code information.

Access

Read-only (virtual property).

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.

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() to determine the exact MIME type.

public function __construct(array $config = [])

UploadedFile constructor.

Parameters

ParameterTypeDescription
$configarrayName-value pairs that will be used to initialize the object properties
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

public function getBaseName(): string

Returns

string: Original file base name

public function getExtension(): string

Returns

string: File extension

public function getHasError(): bool

Returns

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

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

Parameters

ParameterTypeDescription
$modelyii\base\ModelThe data model
$attributestringThe 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

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

ParameterTypeDescription
$namestringThe 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.

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

Returns all uploaded files for the given model attribute.

Parameters

ParameterTypeDescription
$modelyii\base\ModelThe data model
$attributestringThe 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.

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

ParameterTypeDescription
$namestringThe 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.

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

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

ParameterTypeDescription
$filestringThe file path or a path alias used to save the uploaded file.
$deleteTempFileboolWhether 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

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

Copy temporary file into file specified

Parameters

ParameterTypeDescription
$targetFilestringPath of the file to copy to

Returns

int|false: The total count of bytes copied, or false on failure

Since

2.0.32