1 追蹤者

類別 yii\web\UploadedFile

繼承關係yii\web\UploadedFile » yii\base\BaseObject
實作介面yii\base\Configurable
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/web/UploadedFile.php

UploadedFile 代表上傳檔案的資訊。

您可以呼叫 getInstance() 來檢索上傳檔案的實例,然後使用 saveAs() 將其儲存到伺服器上。您也可以查詢關於檔案的其他資訊,包括 $name$tempName$type$size$error$fullPath

關於 UploadedFile 的更多詳細資訊和使用方法,請參閱處理檔案上傳的指南文章

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$baseName string 原始檔案基本名稱。 yii\web\UploadedFile
$error integer 描述此檔案上傳狀態的錯誤代碼。 yii\web\UploadedFile
$extension string 檔案副檔名。 yii\web\UploadedFile
$fullPath string|null 瀏覽器提交的完整路徑。 yii\web\UploadedFile
$hasError boolean 上傳的檔案是否有錯誤。 yii\web\UploadedFile
$name string 正在上傳的檔案原始名稱 yii\web\UploadedFile
$size integer 上傳檔案的實際大小(以位元組為單位) yii\web\UploadedFile
$tempName string 伺服器上上傳檔案的路徑。 yii\web\UploadedFile
$type string 上傳檔案的 MIME 類型(例如 "image/gif")。 yii\web\UploadedFile

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫未定義為類別方法的具名方法。 yii\base\BaseObject
__construct() UploadedFile 建構子。 yii\web\UploadedFile
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__toString() 字串輸出。 yii\web\UploadedFile
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
canGetProperty() 傳回一個值,指示是否可以讀取屬性。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示是否可以設定屬性。 yii\base\BaseObject
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
getBaseName() yii\web\UploadedFile
getExtension() yii\web\UploadedFile
getHasError() yii\web\UploadedFile
getInstance() 為給定的模型屬性傳回一個上傳檔案。 yii\web\UploadedFile
getInstanceByName() 根據給定的檔案輸入名稱傳回一個上傳檔案。 yii\web\UploadedFile
getInstances() 為給定的模型屬性傳回所有上傳檔案。 yii\web\UploadedFile
getInstancesByName() 傳回與指定檔案輸入名稱對應的上傳檔案陣列。 yii\web\UploadedFile
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\base\BaseObject
reset() 清除已載入的 UploadedFile 實例。 yii\web\UploadedFile
saveAs() 儲存上傳的檔案。 yii\web\UploadedFile

保護方法

隱藏繼承的方法

方法 描述 定義於
copyTempFile() 將暫存檔複製到指定檔案 yii\web\UploadedFile

屬性詳細資訊

隱藏繼承的屬性

$baseName 公開屬性

原始檔案基本名稱。

public string $baseName null
$error public property

描述此檔案上傳狀態的錯誤代碼。

另請參閱 https://php.dev.org.tw/manual/en/features.file-upload.errors.php

public integer $error null
$extension public property

檔案副檔名。

public string $extension null
$fullPath public property (自版本 2.0.46 起可用)

瀏覽器提交的完整路徑。請注意,此值並不總是包含真實的目錄結構,因此不可信任。自 PHP 8.1 起可用。

public string|null $fullPath null
$hasError public property

指示上傳檔案是否發生錯誤。請查看 $error 以取得詳細的錯誤代碼資訊。

public boolean $hasError null
$name public property

正在上傳的檔案原始名稱

public string $name null
$size public property

上傳檔案的實際大小(以位元組為單位)

public integer $size null
$tempName public property

伺服器上傳檔案的路徑。請注意,這是一個暫存檔,在目前的請求處理完成後,PHP 會自動刪除它。

public string $tempName null
$type public property

上傳檔案的 MIME 類型(例如 "image/gif")。由於此 MIME 類型未在伺服器端檢查,因此請勿將此值視為理所當然。相反地,請使用 yii\helpers\FileHelper::getMimeType() 來判斷確切的 MIME 類型。

public string $type null

方法詳情

隱藏繼承的方法

__call() public method

定義於: yii\base\BaseObject::__call()

呼叫未定義為類別方法的具名方法。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當調用未知方法時,它會被隱式呼叫。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

return mixed

方法的傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

UploadedFile 建構子。

public void __construct ( $config = [] )
$config array

將用於初始化物件屬性的名稱-值配對

                public function __construct($config = [])
{
    $this->_tempResource = ArrayHelper::remove($config, 'tempResource');
    parent::__construct($config);
}

            
__get() public method

定義於: yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property; 時,它會被隱式呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws yii\base\InvalidCallException

如果屬性為唯寫

                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);
}

            
__isset() public method

定義於: yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property) 時,它會被隱式呼叫。

請注意,如果屬性未定義,將會傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

指示已命名的屬性是否已設定(非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

定義於: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value; 時,它會被隱式呼叫。

另請參閱 __get()

public void __set ( $name, $value )
$name string

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws yii\base\InvalidCallException

如果屬性為唯讀

                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);
    }
}

            
__toString() public method

字串輸出。

這是 PHP 魔術方法,會傳回物件的字串表示形式。此處的實作傳回上傳檔案的名稱。

public string __toString ( )
return string

物件的字串表示形式

                public function __toString()
{
    return $this->name;
}

            
__unset() public method

定義於: yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property) 時,它會被隱式呼叫。

請注意,如果屬性未定義,此方法將不執行任何操作。如果屬性為唯讀,則會擲出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

throws yii\base\InvalidCallException

如果屬性為唯讀。

                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);
    }
}

            
canGetProperty() public method

定義於: yii\base\BaseObject::canGetProperty()

傳回一個值,指示是否可以讀取屬性。

如果符合以下條件,屬性為可讀:

  • 類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可讀

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

定義於: yii\base\BaseObject::canSetProperty()

傳回一個值,指示是否可以設定屬性。

如果符合以下條件,屬性為可寫:

  • 類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可寫

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
自 2.0.14 版本起已棄用。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
copyTempFile() protected method (自版本 2.0.32 起可用)

將暫存檔複製到指定檔案

protected integer|false copyTempFile ( $targetFile )
$targetFile string

要複製到的檔案路徑

return integer|false

複製的總位元組計數,失敗時為 false

                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;
}

            
getBaseName() public method

public string getBaseName ( )
return string

原始檔案基本名稱

                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');
}

            
getExtension() public method

public string getExtension ( )
return string

檔案副檔名

                public function getExtension()
{
    return strtolower(pathinfo($this->name, PATHINFO_EXTENSION));
}

            
getHasError() public method

public boolean getHasError ( )
return boolean

指示上傳檔案是否發生錯誤。請查看 $error 以取得詳細的錯誤代碼資訊。

                public function getHasError()
{
    return $this->error != UPLOAD_ERR_OK;
}

            
getInstance() public static method

為給定的模型屬性傳回一個上傳檔案。

檔案應使用 yii\widgets\ActiveField::fileInput() 上傳。

另請參閱 getInstanceByName()

public static yii\web\UploadedFile|null getInstance ( $model, $attribute )
$model yii\base\Model

資料模型

$attribute string

屬性名稱。屬性名稱可能包含陣列索引。例如,表格檔案上傳的 '[1]file';以及檔案陣列中元素的 'file[1]'。

return yii\web\UploadedFile|null

上傳檔案的實例。如果沒有為指定的模型屬性上傳檔案,則傳回 Null。

                public static function getInstance($model, $attribute)
{
    $name = Html::getInputName($model, $attribute);
    return static::getInstanceByName($name);
}

            
getInstanceByName() public static method

根據給定的檔案輸入名稱傳回一個上傳檔案。

名稱可以是純字串或類似陣列元素的字串(例如 'Post[imageFile]' 或 'Post[0][imageFile]')。

public static yii\web\UploadedFile|null getInstanceByName ( $name )
$name string

檔案輸入欄位的名稱。

return yii\web\UploadedFile|null

上傳檔案的實例。如果沒有為指定的名稱上傳檔案,則傳回 Null。

                public static function getInstanceByName($name)
{
    $files = self::loadFiles();
    return isset($files[$name]) ? new static($files[$name]) : null;
}

            
getInstances() public static method

為給定的模型屬性傳回所有上傳檔案。

public static yii\web\UploadedFile[] getInstances ( $model, $attribute )
$model yii\base\Model

資料模型

$attribute string

屬性名稱。屬性名稱可能包含表格檔案上傳的陣列索引,例如 '[1]file'。

return yii\web\UploadedFile[]

UploadedFile 物件的陣列。如果找不到給定屬性的可用檔案,則傳回空陣列。

                public static function getInstances($model, $attribute)
{
    $name = Html::getInputName($model, $attribute);
    return static::getInstancesByName($name);
}

            
getInstancesByName() public static method

傳回與指定檔案輸入名稱對應的上傳檔案陣列。

當多個檔案被上傳並儲存為 'files[0]'、'files[1]'、'files[n]'... 時,這主要用於透過傳遞 'files' 作為名稱來檢索它們。

public static yii\web\UploadedFile[] getInstancesByName ( $name )
$name string

檔案陣列的名稱

return yii\web\UploadedFile[]

UploadedFile 物件的陣列。如果找不到足夠的上傳,則傳回空陣列。請注意,此陣列將包含來自所有子陣列的所有檔案,無論它們嵌套多深。

                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;
}

            
hasMethod() public method

定義於: yii\base\BaseObject::hasMethod()

傳回一個值,指示是否已定義方法。

預設實作是對 php 函數 method_exists() 的呼叫。當您實作 php 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

return boolean

指示是否已定義方法

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

定義於: yii\base\BaseObject::hasProperty()

傳回一個值,指示是否已定義屬性。

如果符合以下條件,則定義屬性:

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

指示是否已定義屬性

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

定義於: yii\base\BaseObject::init()

初始化物件。

在物件使用給定的組態初始化後,此方法會在建構函式的結尾被調用。

public void init ( )

                public function init()
{
}

            
reset() public static method

清除已載入的 UploadedFile 實例。

此方法主要由測試腳本使用,以建立測試夾具 (fixture)。

public static void reset ( )

                public static function reset()
{
    self::$_files = null;
}

            
saveAs() public method

儲存上傳的檔案。

如果目標檔案 $file 已經存在,它將會被覆寫。

另請參閱 $error

public boolean saveAs ( $file, $deleteTempFile true )
$file string

用於儲存已上傳檔案的檔案路徑或路徑別名。

$deleteTempFile boolean

是否在儲存後刪除暫存檔。 如果為 true,您將無法在當前請求中再次儲存已上傳的檔案。

return boolean

布林值,表示檔案是否成功儲存

                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);
}