UploadedFile
extends BaseObject
in package
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.
Tags
Table of Contents
Properties
- $baseName : string
- $error : int
- $extension : string
- $fullPath : string|null
- $hasError : bool
- $name : string
- $size : int
- $tempName : string
- $type : string
- $_files : array<string|int, array<string|int, mixed>>
- $_tempResource : resource|null
Methods
- __call() : mixed
- Calls the named method which is not a class method.
- __construct() : mixed
- UploadedFile constructor.
- __get() : mixed
- Returns the value of an object property.
- __isset() : bool
- Checks if a property is set, i.e. defined and not null.
- __set() : mixed
- Sets value of an object property.
- __toString() : string
- String output.
- __unset() : mixed
- Sets an object property to null.
- canGetProperty() : bool
- Returns a value indicating whether a property can be read.
- canSetProperty() : bool
- Returns a value indicating whether a property can be set.
- className() : string
- Returns the fully qualified name of this class.
- getBaseName() : string
- getExtension() : string
- getHasError() : bool
- getInstance() : UploadedFile|null
- Returns an uploaded file for the given model attribute.
- getInstanceByName() : UploadedFile|null
- Returns an uploaded file according to the given file input name.
- getInstances() : array<string|int, UploadedFile>
- Returns all uploaded files for the given model attribute.
- getInstancesByName() : array<string|int, UploadedFile>
- Returns an array of uploaded files corresponding to the specified file input name.
- hasMethod() : bool
- Returns a value indicating whether a method is defined.
- hasProperty() : bool
- Returns a value indicating whether a property is defined.
- init() : mixed
- Initializes the object.
- reset() : mixed
- Cleans up the loaded UploadedFile instances.
- saveAs() : bool
- Saves the uploaded file.
- copyTempFile() : int|false
- Copy temporary file into file specified
- loadFiles() : array<string|int, array<string|int, mixed>>
- Returns reformated data of uplodaded files.
- loadFilesRecursive() : mixed
- Recursive reformats data of uplodaded file(s).
Properties
$baseName read-only
public
string
$baseName
Original file base name.
$error
public
int
$error
an error code describing the status of this file uploading.
Tags
$extension read-only
public
string
$extension
File extension.
$fullPath
public
string|null
$fullPath
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.
Tags
$hasError read-only
public
bool
$hasError
Whether there is an error with the uploaded file. Check [[error]] for detailed error code information.
$name
public
string
$name
the original name of the file being uploaded
$size
public
int
$size
the actual size of the uploaded file in bytes
$tempName
public
string
$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
public
string
$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.
$_files
private
static array<string|int, array<string|int, mixed>>
$_files
$_tempResource
private
resource|null
$_tempResource
a temporary uploaded stream resource used within PUT and PATCH request.
Methods
__call()
Calls the named method which is not a class method.
public
__call(string $name, array<string|int, mixed> $params) : mixed
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
Parameters
- $name : string
-
the method name
- $params : array<string|int, mixed>
-
method parameters
Tags
Return values
mixed —the method return value
__construct()
UploadedFile constructor.
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string|int, mixed> = []
-
name-value pairs that will be used to initialize the object properties
__get()
Returns the value of an object property.
public
__get(string $name) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
Parameters
- $name : string
-
the property name
Tags
Return values
mixed —the property value
__isset()
Checks if a property is set, i.e. defined and not null.
public
__isset(string $name) : bool
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property)
.
Note that if the property is not defined, false will be returned.
Parameters
- $name : string
-
the property name or the event name
Tags
Return values
bool —whether the named property is set (not null).
__set()
Sets value of an object property.
public
__set(string $name, mixed $value) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
Parameters
- $name : string
-
the property name or the event name
- $value : mixed
-
the property value
Tags
__toString()
String output.
public
__toString() : string
This is PHP magic method that returns string representation of an object. The implementation here returns the uploaded file's name.
Return values
string —the string representation of the object
__unset()
Sets an object property to null.
public
__unset(string $name) : mixed
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property)
.
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
Parameters
- $name : string
-
the property name
Tags
canGetProperty()
Returns a value indicating whether a property can be read.
public
canGetProperty(string $name[, bool $checkVars = true ]) : bool
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be read
canSetProperty()
Returns a value indicating whether a property can be set.
public
canSetProperty(string $name[, bool $checkVars = true ]) : bool
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property can be written
className()
Returns the fully qualified name of this class.
public
static className() : string
Tags
Return values
string —the fully qualified name of this class.
getBaseName()
public
getBaseName() : string
Return values
string —original file base name
getExtension()
public
getExtension() : string
Return values
string —file extension
getHasError()
public
getHasError() : bool
Return values
bool —whether there is an error with the uploaded file. Check [[error]] for detailed error code information.
getInstance()
Returns an uploaded file for the given model attribute.
public
static getInstance(Model $model, string $attribute) : UploadedFile|null
The file should be uploaded using [[\yii\widgets\ActiveField::fileInput()]].
Parameters
- $model : 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.
Tags
Return values
UploadedFile|null —the instance of the uploaded file. Null is returned if no file is uploaded for the specified model attribute.
getInstanceByName()
Returns an uploaded file according to the given file input name.
public
static getInstanceByName(string $name) : UploadedFile|null
The name can be a plain string or a string like an array element (e.g. 'Post[imageFile]', or 'Post[0][imageFile]').
Parameters
- $name : string
-
the name of the file input field.
Return values
UploadedFile|null —the instance of the uploaded file. Null is returned if no file is uploaded for the specified name.
getInstances()
Returns all uploaded files for the given model attribute.
public
static getInstances(Model $model, string $attribute) : array<string|int, UploadedFile>
Parameters
- $model : 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 values
array<string|int, UploadedFile> —array of UploadedFile objects. Empty array is returned if no available file was found for the given attribute.
getInstancesByName()
Returns an array of uploaded files corresponding to the specified file input name.
public
static getInstancesByName(string $name) : array<string|int, UploadedFile>
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
- $name : string
-
the name of the array of files
Return values
array<string|int, 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.
hasMethod()
Returns a value indicating whether a method is defined.
public
hasMethod(string $name) : bool
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
Parameters
- $name : string
-
the method name
Return values
bool —whether the method is defined
hasProperty()
Returns a value indicating whether a property is defined.
public
hasProperty(string $name[, bool $checkVars = true ]) : bool
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
Tags
Return values
bool —whether the property is defined
init()
Initializes the object.
public
init() : mixed
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
reset()
Cleans up the loaded UploadedFile instances.
public
static reset() : mixed
This method is mainly used by test scripts to set up a fixture.
saveAs()
Saves the uploaded file.
public
saveAs(string $file[, bool $deleteTempFile = true ]) : bool
If the target file $file
already exists, it will be overwritten.
Parameters
- $file : string
-
the file path or a path alias used to save the uploaded file.
- $deleteTempFile : bool = true
-
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.
Tags
Return values
bool —true whether the file is saved successfully
copyTempFile()
Copy temporary file into file specified
protected
copyTempFile(string $targetFile) : int|false
Parameters
- $targetFile : string
-
path of the file to copy to
Tags
Return values
int|false —the total count of bytes copied, or false on failure
loadFiles()
Returns reformated data of uplodaded files.
private
static loadFiles() : array<string|int, array<string|int, mixed>>
Return values
array<string|int, array<string|int, mixed>>loadFilesRecursive()
Recursive reformats data of uplodaded file(s).
private
static loadFilesRecursive(string $key, array<string|int, string>|string $names, array<string|int, string>|string $tempNames, array<string|int, string>|string $types, array<string|int, int>|int $sizes, array<string|int, int>|int $errors, array<string|int, mixed>|string|null $fullPaths, array<string|int, mixed>|resource|null $tempResources) : mixed
Parameters
- $key : string
-
key for identifying uploaded file(sub-array index)
- $names : array<string|int, string>|string
-
file name(s) provided by PHP
- $tempNames : array<string|int, string>|string
-
temporary file name(s) provided by PHP
- $types : array<string|int, string>|string
-
file type(s) provided by PHP
- $sizes : array<string|int, int>|int
-
file size(s) provided by PHP
- $errors : array<string|int, int>|int
-
uploading issue(s) provided by PHP
- $fullPaths : array<string|int, mixed>|string|null
-
the full path(s) as submitted by the browser/PHP
- $tempResources : array<string|int, mixed>|resource|null
-
the resource(s)