| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\librarys\storage;
- class UploadFile extends \yii\web\UploadedFile
- {
- protected $driver;
- protected $basePath;
- /**
- * @param $file
- * @param bool $deleteTempFile
- * @return bool
- */
- public function saveAs($file, $deleteTempFile = true)
- {
- $result = false;
- // Todo is_uploaded_file($this->tempName)
- if ($this->error == UPLOAD_ERR_OK) {
- $result = $this->driver->saveFile($this->tempName, $this->getFullPath($file));
- }
- if ($result && $deleteTempFile) {
- $this->deleteTempFile();
- }
- return $result;
- }
- /**
- * @param $baseName
- * @param bool $deleteTempFile
- * @return bool|string
- */
- public function saveWithOriginalExtension($baseName, $deleteTempFile = true)
- {
- return $this->saveAs($baseName . '.' . $this->getExtension(), $deleteTempFile);
- }
- /**
- * @param $file
- * @return string
- */
- protected function getFullPath($file)
- {
- return rtrim($this->basePath, '/')
- . '/'
- . ltrim($file, '/');
- }
- /**
- * @return bool
- */
- public function deleteTempFile()
- {
- return unlink($this->tempName);
- }
- /**
- * @param $name
- * @param $driver
- * @param $basePath
- * @return \yii\web\UploadedFile[]
- */
- public static function getInstancesByStorage($name, $driver, $basePath)
- {
- parent::reset();
- $instances = parent::getInstancesByName($name);
- foreach ($instances as $key => $instance) {
- if ($instance instanceof self) {
- $instance->driver = $driver;
- $instance->basePath = $basePath;
- continue;
- }
- unset($instances[$key]);
- }
- return $instances;
- }
- /**
- * @param $name
- * @param $driver
- * @param $basePath
- * @return mixed|\yii\web\UploadedFile|static|null
- */
- public static function getInstanceByStorage($name, $driver, $basePath)
- {
- parent::reset();
- $instance = parent::getInstanceByName($name);
- if ($instance instanceof self) {
- $instance->driver = $driver;
- $instance->basePath = $basePath;
- }
- return $instance;
- }
- }
|