Class yii\web\UploadedFile
Inheritance | yii\web\UploadedFile » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/web/UploadedFile.php |
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.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$baseName | string | Original file base name. | yii\web\UploadedFile |
$error | integer | An error code describing the status of this file uploading. | yii\web\UploadedFile |
$extension | string | File extension. | yii\web\UploadedFile |
$fullPath | string|null | The full path as submitted by the browser. | yii\web\UploadedFile |
$hasError | boolean | Whether there is an error with the uploaded file. | yii\web\UploadedFile |
$name | string | The original name of the file being uploaded | yii\web\UploadedFile |
$size | integer | The actual size of the uploaded file in bytes | yii\web\UploadedFile |
$tempName | string | The path of the uploaded file on the server. | yii\web\UploadedFile |
$type | string | The MIME-type of the uploaded file (such as "image/gif"). | yii\web\UploadedFile |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | UploadedFile constructor. | yii\web\UploadedFile |
__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. | yii\web\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() | Returns the fully qualified name of this class. | yii\base\BaseObject |
getBaseName() | yii\web\UploadedFile | |
getExtension() | yii\web\UploadedFile | |
getHasError() | yii\web\UploadedFile | |
getInstance() | Returns an uploaded file for the given model attribute. | yii\web\UploadedFile |
getInstanceByName() | Returns an uploaded file according to the given file input name. | yii\web\UploadedFile |
getInstances() | Returns all uploaded files for the given model attribute. | yii\web\UploadedFile |
getInstancesByName() | Returns an array of uploaded files corresponding to the specified file input name. | yii\web\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() | Cleans up the loaded UploadedFile instances. | yii\web\UploadedFile |
saveAs() | Saves the uploaded file. | yii\web\UploadedFile |
Protected Methods
Method | Description | Defined By |
---|---|---|
copyTempFile() | Copy temporary file into file specified | yii\web\UploadedFile |
Property Details
An error code describing the status of this file uploading.
See also https://www.php.net/manual/en/features.file-upload.errors.php.
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.
Whether there is an error with the uploaded file. Check $error for detailed error code information.
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.
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.
Method Details
Defined in: yii\base\BaseObject::__call()
Calls the named method which is not a class method.
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.
public mixed __call ( $name, $params ) | ||
$name | string |
The method name |
$params | array |
Method parameters |
return | mixed |
The method return value |
---|---|---|
throws | yii\base\UnknownMethodException |
when calling unknown method |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
UploadedFile constructor.
public void __construct ( $config = [] ) | ||
$config | array |
Name-value pairs that will be used to initialize the object properties |
public function __construct($config = [])
{
$this->_tempResource = ArrayHelper::remove($config, 'tempResource');
parent::__construct($config);
}
Defined in: yii\base\BaseObject::__get()
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
See also __set().
public mixed __get ( $name ) | ||
$name | string |
The property name |
return | mixed |
The property value |
---|---|---|
throws | yii\base\UnknownPropertyException |
if the property is not defined |
throws | yii\base\InvalidCallException |
if the property is write-only |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\BaseObject::__isset()
Checks if a property is set, i.e. defined and not null.
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.
public boolean __isset ( $name ) | ||
$name | string |
The property name or the event name |
return | boolean |
Whether the named property is set (not null). |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in: yii\base\BaseObject::__set()
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
See also __get().
public void __set ( $name, $value ) | ||
$name | string |
The property name or the event name |
$value | mixed |
The property value |
throws | yii\base\UnknownPropertyException |
if the property is not defined |
---|---|---|
throws | yii\base\InvalidCallException |
if the property is read-only |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
String output.
This is PHP magic method that returns string representation of an object. The implementation here returns the uploaded file's name.
public string __toString ( ) | ||
return | string |
The string representation of the object |
---|
public function __toString()
{
return $this->name;
}
Defined in: yii\base\BaseObject::__unset()
Sets an object property to null.
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.
public void __unset ( $name ) | ||
$name | string |
The property name |
throws | yii\base\InvalidCallException |
if the property is read only. |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::canGetProperty()
Returns a value indicating whether a property can be read.
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);
See also canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be read |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in: yii\base\BaseObject::canSetProperty()
Returns a value indicating whether a property can be set.
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);
See also canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be written |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
instead.
Defined in: yii\base\BaseObject::className()
Returns the fully qualified name of this class.
public static string className ( ) | ||
return | string |
The fully qualified name of this class. |
---|
public static function className()
{
return get_called_class();
}
Copy temporary file into file specified
protected integer|false copyTempFile ( $targetFile ) | ||
$targetFile | string |
Path of the file to copy to |
return | integer|false |
The total count of bytes copied, or false on failure |
---|
protected function copyTempFile($targetFile)
{
$target = fopen($targetFile, 'wb');
if ($target === false) {
return false;
}
$result = stream_copy_to_stream($this->_tempResource, $target);
@fclose($target);
return $result;
}
public string getBaseName ( ) | ||
return | string |
Original file base name |
---|
public function getBaseName()
{
// https://github.com/yiisoft/yii2/issues/11012
$pathInfo = pathinfo('_' . $this->name, PATHINFO_FILENAME);
return mb_substr($pathInfo, 1, mb_strlen($pathInfo, '8bit'), '8bit');
}
public string getExtension ( ) | ||
return | string |
File extension |
---|
public function getExtension()
{
return strtolower(pathinfo($this->name, PATHINFO_EXTENSION));
}
public boolean getHasError ( ) | ||
return | boolean |
Whether there is an error with the uploaded file. Check $error for detailed error code information. |
---|
public function getHasError()
{
return $this->error != UPLOAD_ERR_OK;
}
Returns an uploaded file for the given model attribute.
The file should be uploaded using yii\widgets\ActiveField::fileInput().
See also getInstanceByName().
public static yii\web\UploadedFile|null getInstance ( $model, $attribute ) | ||
$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 | yii\web\UploadedFile|null |
The instance of the uploaded file. Null is returned if no file is uploaded for the specified model attribute. |
---|
public static function getInstance($model, $attribute)
{
$name = Html::getInputName($model, $attribute);
return static::getInstanceByName($name);
}
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]').
public static yii\web\UploadedFile|null getInstanceByName ( $name ) | ||
$name | string |
The name of the file input field. |
return | yii\web\UploadedFile|null |
The instance of the uploaded file. Null is returned if no file is uploaded for the specified name. |
---|
public static function getInstanceByName($name)
{
$files = self::loadFiles();
return isset($files[$name]) ? new static($files[$name]) : null;
}
Returns all uploaded files for the given model attribute.
public static yii\web\UploadedFile[] getInstances ( $model, $attribute ) | ||
$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 | yii\web\UploadedFile[] |
Array of UploadedFile objects. Empty array is returned if no available file was found for the given attribute. |
---|
public static function getInstances($model, $attribute)
{
$name = Html::getInputName($model, $attribute);
return static::getInstancesByName($name);
}
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.
public static yii\web\UploadedFile[] getInstancesByName ( $name ) | ||
$name | string |
The name of the array of files |
return | yii\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 sub-arrays regardless how deeply nested they are. |
---|
public static function getInstancesByName($name)
{
$files = self::loadFiles();
if (isset($files[$name])) {
return [new static($files[$name])];
}
$results = [];
foreach ($files as $key => $file) {
if (strpos($key, "{$name}[") === 0) {
$results[] = new static($file);
}
}
return $results;
}
Defined in: yii\base\BaseObject::hasMethod()
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
public boolean hasMethod ( $name ) | ||
$name | string |
The method name |
return | boolean |
Whether the method is defined |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
Defined in: yii\base\BaseObject::hasProperty()
Returns a value indicating whether a property is defined.
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);
See also:
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property is defined |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
Defined in: yii\base\BaseObject::init()
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) |
public function init()
{
}
Cleans up the loaded UploadedFile instances.
This method is mainly used by test scripts to set up a fixture.
public static void reset ( ) |
public static function reset()
{
self::$_files = null;
}
Saves the uploaded file.
If the target file $file
already exists, it will be overwritten.
See also $error.
public boolean saveAs ( $file, $deleteTempFile = true ) | ||
$file | string |
The file path or a path alias 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 |
---|
public function saveAs($file, $deleteTempFile = true)
{
if ($this->hasError) {
return false;
}
$targetFile = Yii::getAlias($file);
if (is_resource($this->_tempResource)) {
$result = $this->copyTempFile($targetFile);
return $deleteTempFile ? @fclose($this->_tempResource) : (bool) $result;
}
return $deleteTempFile ? move_uploaded_file($this->tempName, $targetFile) : copy($this->tempName, $targetFile);
}