Commit 286ccde7 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/optimize' into 'master'

fix See merge request !435
parents c293a438 8b47ab26
<?php
namespace api\controllers;
use Yii;
use OSS\OssClient;
use yii\web\UploadedFile;
use api\base\BaseController;
class OssController extends BaseController
{
protected $accessKeyId = "LTAI4FxZ787zpBmjLmr6yMwA";
protected $accessKeySecret = "5OMu030RFIE2KP3fNJrhVRTlVqBLaE";
protected $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
protected $bucket = "bqbwallet";
public function actionUpload()
{
$type = Yii::$app->request->post('type', '');
$uploaded_file = UploadedFile::getInstanceByName('file');
if (false == $uploaded_file || false == $type) {
$this->msg = '参数错误';
$this->code = -1;
goto doEnd;
}
$object = md5(date('Y-m-d H:i:s') . $uploaded_file->tempName) . '.' . $uploaded_file->extension;
$filePath = $uploaded_file->tempName;
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$result = $ossClient->uploadFile($this->bucket, 'upload/' . (empty($type) ? '' : $type . '/') . $object, $filePath);
if (!isset($result["info"]['url'])) {
$this->msg = '上传失败';
$this->code = -1;
goto doEnd;
}
$this->data = $result["info"]['url'];
doEnd :
return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data];
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment