UploadedFile

Type
Class
Namespace
craft\web
Inherits
craft\web\UploadedFile » yii\web\UploadedFile » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0

UploadedFile represents the information for an uploaded file.

View source

Public Properties

PropertyDescription
baseNamestring – Original file base name
errorinteger – An error code describing the status of this file uploading.
extensionstring – File extension
hasErrorboolean – Whether there is an error with the uploaded file.
mimeTypestring, null
namestring – The original name of the file being uploaded
sizeinteger – The actual size of the uploaded file in bytes
tempNamestring – The path of the uploaded file on the server.
typestring – The MIME-type of the uploaded file (such as "image/gif").

mimeType

Type
string, null
Access
Read-only

View source

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__construct()Constructor.
__get()Returns the value of an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__toString()String output.
__unset()Sets an object property to null.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
getBaseName()
getExtension()
getHasError()
getInstance()Returns an uploaded file for the given model attribute.
getInstanceByName()Returns an instance of the specified uploaded file. The name can be a plain string or a string like an array element (e.g. 'Post[imageFile]', or 'Post[0][imageFile]').
getInstances()Returns all uploaded files for the given model attribute.
getInstancesByName()Returns an array of instances starting with specified array name.
getMimeType()Returns the MIME type of the file, based on craft\helpers\FileHelper::getMimeType() rather than what the request told us.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
init()Initializes the object.
reset()Cleans up the loaded UploadedFile instances.
saveAs()Saves the uploaded file.
saveAsTempFile()Saves the uploaded file to a temp location.

getInstanceByName()

Returns an instance of the specified uploaded file. The name can be a plain string or a string like an array element (e.g. 'Post[imageFile]', or 'Post[0][imageFile]').

View source

Arguments

  • $name (string) – The name of the file input field
  • $ensureTempFileExists (boolean) – Whether to only return the instance if its temp files still exists

Returns

static, null – The instance of the uploaded file. null is returned if no file is uploaded for the specified name.

getInstancesByName()

Returns an array of instances starting with specified array name. If multiple files were uploaded and saved as 'Files[0]', 'Files[1]', 'Files[n]'..., you can have them all by passing 'Files' as array name.

View source

Arguments

  • $name (string) – The name of the array of files
  • $lookForSingleInstance (boolean) – If set to true, will look for a single instance of the given name.
  • $ensureTempFilesExist (boolean) – Whether only instances whose temp files still exist should be returned.

Returns

craft\web\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 subarrays regardless how deeply nested they are.

getMimeType()

Since
3.1.7

Returns the MIME type of the file, based on craft\helpers\FileHelper::getMimeType() rather than what the request told us.

View source

Arguments

  • $magicFile (string, null) – Name of the optional magic database file (or alias).
  • $checkExtension (boolean) – Whether to use the file extension to determine the MIME type in case finfo_open() cannot determine it.

Returns

string, null

Throws

saveAsTempFile()

Saves the uploaded file to a temp location.

See also errorView source

Arguments

  • $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.

Returns

string, false – The path to the temp file, or false if the file wasn't saved successfully