tp上传到第三方oss

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/5
 * Time: 22:43
 */
namespace osc\index\controller;
use osc\common\controller\HomeBase;
use base\Oss;
class Test extends HomeBase {
    public function index() {
        if (request()->isPost()) {
            try {
                $file = request()->file('image');
                if (empty($file)) {
                    die('请选择上传的文件');
                }
                $allow_max_size = 2 * pow(1024, 2);
                $allow_upload_ext = ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'wbmp'];
                $path = ROOT_PATH . 'public' . DS . 'uploads';
                $info = $file->validate(['size' => $allow_max_size, 'ext' => $allow_upload_ext])->move($path);
                if (!$info) {
                    var_dump($file->getError());
                    die();
                }
                $fileName = 'uploads/' . $info->getSaveName();
                $ossClient = Oss::getInstance();
                $bucket = Oss::getBucketName();
                $ossClient->uploadFile($bucket, $fileName, $info->getPathname());
            } catch (OssException $e) {
                return $e->getMessage();
            }
        } else {
            return $this->fetch();
        }
    }
}

  

posted @ 2023-10-09 10:17  星云惊蛰  阅读(9)  评论(0)    收藏  举报