public function actionIndex() { //include "../../vendor/aliyuncs/oss-sdk-php/src/OSS/OssClient.php"; //$OSS = new \OSS\OssClient(Yii::$app->params['oss-config']['OSS_ACCESS_ID'], Yii::$app->params['oss-config']['OSS_ACCESS_KEY'],Yii::$app->params['oss-config']['OSS_ENDPOINT']); Oss::upload('images/1.jpg', '1.jpg', __FILE__); echo OSS::getUrl('images/1.jpg'); die; //$file = $OSS->uploadFile('welcomedcoffee',"1.jpg", __FILE__); //var_dump($file);DIE; return $this->render('index'); }
/** * 上传方法, 缩略图前缀为 big_ medium_ small_ * @params string $size 上传大小限制 * @params array $width 缩略图宽度尺寸 * @params array $height 缩略图高度尺寸 * @return null */ public function upload($size = 3145728, $width = ['50', '200', '400'], $height = ['50', '200', '400']) { //设置上传限制 $this->maxSize = $size; $this->allowExts = array('jpg', 'gif', 'png', 'jpeg'); //设置保存目录 $this->savePath = './public/uploads/'; //启用子目录保存文件 $this->autoSub = true; //子目录创建方式 可以使用hash date custom $this->subType = 'date'; //设置生成缩略图 $this->thumb = true; $this->thumbMaxWidth = implode(',', $width); $this->thumbMaxHeight = implode(',', $height); $thumbFile = ['small_', 'medium_', 'big_']; $this->thumbFile = implode(',', $thumbFile); //var_dump($this->instance);die; //获取上传结果 if (!$this->instance->upload()) { $this->errorMsg = $this->instance->getErrorMsg(); return false; } else { $fileInfo = $this->instance->getUploadFileInfo(); if ($this->isOss) { //上传缩略图 foreach ($fileInfo as $index => $item) { foreach ($thumbFile as $key => $value) { $object = str_replace('/', '/' . $value, $fileInfo[$index]['savename']); $filePath = $fileInfo[$index]['savepath'] . $object; if (Oss::uploadFile($object, $filePath)) { //删除原图 unlink($filePath); } else { $this->errorMsg = Oss::$error; return false; } } } $this->successMsg = $fileInfo; return true; //删除本地图片 } else { $this->successMsg = $fileInfo; return true; } } }