類別 yii\web\MultipartFormDataParser
MultipartFormDataParser 解析編碼為 'multipart/form-data' 的內容。
此解析器為非 POST 請求(例如:具有 'PUT' 請求方法的請求)上的 'multipart/form-data' 處理提供後備方案。
為了啟用此解析器,您應該以下列方式設定 yii\web\Request::$parsers
return [
'components' => [
'request' => [
'parsers' => [
'multipart/form-data' => 'yii\web\MultipartFormDataParser'
],
],
// ...
],
// ...
];
此解析器的 parse() 方法會自動使用從原始主體解析的檔案來填充 $_FILES
。
注意:由於這是一個請求解析器,它將在 yii\web\Request::getBodyParams() 上初始化
$_FILES
值。在調用此方法之前,即使請求主體中提交了檔案,$_FILES
陣列仍將保持為空。如果您正在使用此解析器,請確保在嘗試取得上傳檔案之前已請求主體參數。
使用範例
use yii\web\UploadedFile;
$restRequestData = Yii::$app->request->getBodyParams();
$uploadedFile = UploadedFile::getInstancesByName('photo');
$model = new Item();
$model->populate($restRequestData);
copy($uploadedFile->tempName, '/path/to/file/storage/photo.jpg');
注意:雖然此解析器完全模擬了
$_FILES
的常規結構,但通過tmp_name
鍵可用的相關臨時檔案將不會被 PHP 識別為已上傳的檔案。因此,諸如is_uploaded_file()
和move_uploaded_file()
之類的函數將在它們身上失敗。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$force | boolean | 即使對於 'POST' 請求和 $_FILES 已填充的情況,是否仍解析原始請求主體。 |
yii\web\MultipartFormDataParser |
$uploadFileMaxCount | integer | 最大上傳檔案數量。 | yii\web\MultipartFormDataParser |
$uploadFileMaxSize | integer | 上傳檔案最大尺寸 (bytes)。 | yii\web\MultipartFormDataParser |
公開方法
方法 | 描述 | 定義於 |
---|---|---|
__call() | 呼叫指定的非類別方法。 | yii\base\BaseObject |
__construct() | 建構子。 | yii\base\BaseObject |
__get() | 傳回物件屬性的值。 | yii\base\BaseObject |
__isset() | 檢查屬性是否已設定,即已定義且非 null。 | yii\base\BaseObject |
__set() | 設定物件屬性的值。 | yii\base\BaseObject |
__unset() | 將物件屬性設為 null。 | yii\base\BaseObject |
canGetProperty() | 傳回表示屬性是否可讀取的值。 | yii\base\BaseObject |
canSetProperty() | 傳回表示屬性是否可設定的值。 | yii\base\BaseObject |
className() | 傳回此類別的完整限定名稱。 | yii\base\BaseObject |
getUploadFileMaxCount() | yii\web\MultipartFormDataParser | |
getUploadFileMaxSize() | yii\web\MultipartFormDataParser | |
hasMethod() | 傳回表示方法是否已定義的值。 | yii\base\BaseObject |
hasProperty() | 傳回表示屬性是否已定義的值。 | yii\base\BaseObject |
init() | 初始化物件。 | yii\base\BaseObject |
parse() | 解析 HTTP 請求主體。 | yii\web\MultipartFormDataParser |
setUploadFileMaxCount() | yii\web\MultipartFormDataParser | |
setUploadFileMaxSize() | yii\web\MultipartFormDataParser |
屬性詳細資訊
即使對於 'POST' 請求和 $_FILES
已填充的情況,是否仍解析原始請求主體。預設情況下,此選項為停用,以節省 'POST' 請求的效能,因為 PHP 已自動處理這些請求。 > 注意:如果啟用此選項,則每次解析時都會重設 $_FILES 的值。
Method Details
方法詳情
Defined in: yii\base\BaseObject::__call()
定義於: yii\base\BaseObject::__call()
呼叫指定的非類別方法。
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.
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當調用未知方法時會被隱式呼叫。
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()");
}
Defined in: yii\base\BaseObject::__construct()
定義於: yii\base\BaseObject::__construct()
建構子。
The default implementation does two things
預設實作會做兩件事
- Initializes the object with the given configuration
$config
.使用給定的組態
$config
初始化物件。 - Call init().
呼叫 init()。
If this method is overridden in a child class, it is recommended that
如果這個方法在子類別中被覆寫,建議
- the last parameter of the constructor is a configuration array, like
$config
here.建構子的最後一個參數是一個組態陣列,就像此處的
$config
一樣。 - call the parent implementation at the end of the constructor.
在建構子的結尾呼叫父類別的實作。
public void __construct ( $config = [] ) | ||
$config | array |
Name-value pairs that will be used to initialize the object properties 將用於初始化物件屬性的名稱-值對 |
原始碼
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
Defined in: yii\base\BaseObject::__get()
定義於: yii\base\BaseObject::__get()
傳回物件屬性的值。
Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;
.
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property;
時會被隱式呼叫。
See also __set().
另請參閱 __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()
定義於: yii\base\BaseObject::__isset()
檢查屬性是否已設定,即已定義且非 null。
Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property)
.
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property)
時會被隱式呼叫。
Note that if the property is not defined, false will be returned.
請注意,如果屬性未定義,將會回傳 false。
See also https://php.dev.org.tw/manual/en/function.isset.php.
public boolean __isset ( $name ) | ||
$name | string |
The property name or the event name 屬性名稱或事件名稱 |
return 回傳 |
boolean |
Whether the named property is set (not null). 具名屬性是否已設定(非 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()
定義於: yii\base\BaseObject::__set()
設定物件屬性的值。
Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;
.
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value;
時會被隱式呼叫。
See also __get().
另請參閱 __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);
}
}
Defined in: yii\base\BaseObject::__unset()
定義於: yii\base\BaseObject::__unset()
將物件屬性設為 null。
Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property)
.
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 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.
請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性為唯讀,它將拋出例外。
See also https://php.dev.org.tw/manual/en/function.unset.php.
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()
定義於: yii\base\BaseObject::canGetProperty()
傳回表示屬性是否可讀取的值。
A property is readable if
如果符合以下條件,屬性是可讀的
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
- the class has a member variable with the specified name (when
$checkVars
is true);類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
See also canSetProperty().
另請參閱 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()
定義於: yii\base\BaseObject::canSetProperty()
傳回表示屬性是否可設定的值。
A property is writable if
如果符合以下條件,屬性是可寫的
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
- the class has a member variable with the specified name (when
$checkVars
is true);類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
See also canGetProperty().
另請參閱 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.自 2.0.14 版本起已棄用。在 PHP >=5.5 版本上,請改用 ::class
。
public static string className ( ) | ||
return 回傳 |
string |
The fully qualified name of this class. 這個類別的完整限定名稱。 |
---|
原始碼
public static function className()
{
return get_called_class();
}
public integer getUploadFileMaxCount ( ) | ||
return 回傳 |
integer |
最大上傳檔案數量。 |
---|
原始碼
public function getUploadFileMaxCount()
{
if ($this->_uploadFileMaxCount === null) {
$this->_uploadFileMaxCount = (int)ini_get('max_file_uploads');
}
return $this->_uploadFileMaxCount;
}
public integer getUploadFileMaxSize ( ) | ||
return 回傳 |
integer |
上傳檔案最大尺寸 (bytes)。 |
---|
原始碼
public function getUploadFileMaxSize()
{
if ($this->_uploadFileMaxSize === null) {
$this->_uploadFileMaxSize = $this->getByteSize(ini_get('upload_max_filesize'));
}
return $this->_uploadFileMaxSize;
}
Defined in: yii\base\BaseObject::hasMethod()
定義於: yii\base\BaseObject::hasMethod()
傳回表示方法是否已定義的值。
The default implementation is a call to php function method_exists()
. You may override this method when you implemented the php magic method __call()
.
預設實作是對 php 函數 method_exists()
的呼叫。當您實作了 php 魔術方法 __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()
定義於: yii\base\BaseObject::hasProperty()
傳回表示屬性是否已定義的值。
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);
類別具有與指定名稱相關聯的 getter 或 setter 方法(在此情況下,屬性名稱不區分大小寫);
- the class has a member variable with the specified name (when
$checkVars
is true);類別具有具有指定名稱的成員變數(當
$checkVars
為 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()
定義於: yii\base\BaseObject::init()
初始化物件。
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()
{
}
解析 HTTP 請求主體。
public array parse ( $rawBody, $contentType ) | ||
$rawBody | string |
The raw HTTP request body. 原始 HTTP 請求主體。 |
$contentType | string |
The content type specified for the request body. 為請求主體指定的內容類型。 |
return 回傳 |
array |
Parameters parsed from the request body 從請求主體解析的參數 |
---|
原始碼
public function parse($rawBody, $contentType)
{
if (!$this->force) {
if (!empty($_POST) || !empty($_FILES)) {
// normal POST request is parsed by PHP automatically
return $_POST;
}
} else {
$_FILES = [];
}
if (empty($rawBody)) {
return [];
}
if (!preg_match('/boundary="?(.*)"?$/is', $contentType, $matches)) {
return [];
}
$boundary = trim($matches[1], '"');
$bodyParts = preg_split('/\\R?-+' . preg_quote($boundary, '/') . '/s', $rawBody);
array_pop($bodyParts); // last block always has no data, contains boundary ending like `--`
$bodyParams = [];
$filesCount = 0;
foreach ($bodyParts as $bodyPart) {
if (empty($bodyPart)) {
continue;
}
list($headers, $value) = preg_split('/\\R\\R/', $bodyPart, 2);
$headers = $this->parseHeaders($headers);
if (!isset($headers['content-disposition']['name'])) {
continue;
}
if (isset($headers['content-disposition']['filename'])) {
// file upload:
if ($filesCount >= $this->getUploadFileMaxCount()) {
continue;
}
$fileInfo = [
'name' => $headers['content-disposition']['filename'],
'type' => ArrayHelper::getValue($headers, 'content-type', 'application/octet-stream'),
'size' => StringHelper::byteLength($value),
'error' => UPLOAD_ERR_OK,
'tmp_name' => null,
];
if ($fileInfo['size'] > $this->getUploadFileMaxSize()) {
$fileInfo['error'] = UPLOAD_ERR_INI_SIZE;
} else {
$tmpResource = tmpfile();
if ($tmpResource === false) {
$fileInfo['error'] = UPLOAD_ERR_CANT_WRITE;
} else {
$tmpResourceMetaData = stream_get_meta_data($tmpResource);
$tmpFileName = $tmpResourceMetaData['uri'];
if (empty($tmpFileName)) {
$fileInfo['error'] = UPLOAD_ERR_CANT_WRITE;
@fclose($tmpResource);
} else {
fwrite($tmpResource, $value);
rewind($tmpResource);
$fileInfo['tmp_name'] = $tmpFileName;
$fileInfo['tmp_resource'] = $tmpResource; // save file resource, otherwise it will be deleted
}
}
}
$this->addFile($_FILES, $headers['content-disposition']['name'], $fileInfo);
$filesCount++;
} else {
// regular parameter:
$this->addValue($bodyParams, $headers['content-disposition']['name'], $value);
}
}
return $bodyParams;
}
public void setUploadFileMaxCount ( $uploadFileMaxCount ) | ||
$uploadFileMaxCount | integer |
最大上傳檔案數量。 |
原始碼
public function setUploadFileMaxCount($uploadFileMaxCount)
{
$this->_uploadFileMaxCount = $uploadFileMaxCount;
}
public void setUploadFileMaxSize ( $uploadFileMaxSize ) | ||
$uploadFileMaxSize | integer |
上傳檔案最大尺寸 (bytes)。 |
原始碼
public function setUploadFileMaxSize($uploadFileMaxSize)
{
$this->_uploadFileMaxSize = $uploadFileMaxSize;
}
使用者貢獻筆記
留下評論
Signup or Login in order to comment.
註冊 或 登入 以發表評論。