PHP Class yii\web\UploadedFile

You can call UploadedFile::getInstance to retrieve the instance of an uploaded file, and then use UploadedFile::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.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Object
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$error an error code describing the status of this file uploading.
$name the original name of the file being uploaded
$size the actual size of the uploaded file in bytes
$tempName 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 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 Methods

Method Description
__toString ( ) : string String output.
getBaseName ( ) : string
getExtension ( ) : string
getHasError ( ) : boolean
getInstance ( Model $model, string $attribute ) : UploadedFile Returns an uploaded file for the given model attribute.
getInstanceByName ( string $name ) : null | UploadedFile Returns an uploaded file according to the given file input name.
getInstances ( Model $model, string $attribute ) : UploadedFile[] Returns all uploaded files for the given model attribute.
getInstancesByName ( string $name ) : UploadedFile[] Returns an array of uploaded files corresponding to the specified file input name.
reset ( ) Cleans up the loaded UploadedFile instances.
saveAs ( string $file, boolean $deleteTempFile = true ) : boolean Saves the uploaded file.

Private Methods

Method Description
loadFiles ( ) : array Creates UploadedFile instances from $_FILE.
loadFilesRecursive ( string $key, mixed $names, mixed $tempNames, mixed $types, mixed $sizes, mixed $errors ) Creates UploadedFile instances from $_FILE recursively.

Method Details

__toString() public method

This is PHP magic method that returns string representation of an object. The implementation here returns the uploaded file's name.
public __toString ( ) : string
return string the string representation of the object

getBaseName() public method

public getBaseName ( ) : string
return string original file base name

getExtension() public method

public getExtension ( ) : string
return string file extension

getHasError() public method

public getHasError ( ) : boolean
return boolean whether there is an error with the uploaded file. Check [[error]] for detailed error code information.

getInstance() public static method

The file should be uploaded using [[\yii\widgets\ActiveField::fileInput()]].
See also: getInstanceByName()
public static getInstance ( Model $model, string $attribute ) : UploadedFile
$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 UploadedFile the instance of the uploaded file. Null is returned if no file is uploaded for the specified model attribute.

getInstanceByName() public static method

The name can be a plain string or a string like an array element (e.g. 'Post[imageFile]', or 'Post[0][imageFile]').
public static getInstanceByName ( string $name ) : null | UploadedFile
$name string the name of the file input field.
return null | 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 getInstances ( Model $model, string $attribute ) : UploadedFile[]
$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 UploadedFile[] array of UploadedFile objects. Empty array is returned if no available file was found for the given attribute.

getInstancesByName() public static method

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 getInstancesByName ( string $name ) : UploadedFile[]
$name string the name of the array of files
return 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

This method is mainly used by test scripts to set up a fixture.
public static reset ( )

saveAs() public method

Note that this method uses php's move_uploaded_file() method. If the target file $file already exists, it will be overwritten.
See also: error
public saveAs ( string $file, boolean $deleteTempFile = true ) : boolean
$file string the file path 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

Property Details

$error public property

an error code describing the status of this file uploading.
See also: http://www.php.net/manual/en/features.file-upload.errors.php
public $error

$name public property

the original name of the file being uploaded
public $name

$size public property

the actual size of the uploaded file in bytes
public $size

$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 $tempName

$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 $type