扩展开发-上传扩展

目前已有本地上传、阿里云OSS上传、腾讯云COS上传、七牛云上传扩展

扩展入口文件

文件目录 crmeb\services\upload\Upload.php

namespace crmeb\services\upload;

use crmeb\basic\BaseManager;
use think\facade\Config;

/**
 * Class Upload
 * @package crmeb\services\upload
 * @mixin \crmeb\services\upload\storage\Local
 * @mixin \crmeb\services\upload\storage\OSS
 * @mixin \crmeb\services\upload\storage\COS
 * @mixin \crmeb\services\upload\storage\Qiniu
 */
class Upload extends BaseManager
{
    //...
}

使用上传扩展

use crmeb\services\upload\Upload;

$upload = new Upload();

//上传文件
$path = '上传目录';
//validate() 方法为验证上传
//move() 调用move方法进行上传
$res = $upload->to($path)->validate()->move();

//上传文件流
$fileContent = '文件流内容';
$res = $upload->to($path)->validate()->stream($fileContent);

//上传文件流不验证文件
$res = $upload->to($path)->stream($fileContent);

获取第三方上传桶列表

$type 1=本地上传,2=七牛云、3=阿里云oss、4=腾讯云cos

use app\services\other\UploadService;

//切换到阿里云oss上传
$type = 3;

$upload = UploadService::init($type);

//获取桶列表
$list = $upload->listbuckets();

var_dump($list);

/*
[
    {
        "name":"桶名",
        "location":"区域",
        "createTime":"创建时间",
    }
]
*/

创建第三方上传扩展桶

use app\services\other\UploadService;

//切换到阿里云oss上传
$type = 3;

$upload = UploadService::init($type);

//创建桶
$res = $upload->createBucket('test-bucket-oss');

if (false === $res) {
    var_dump($upload->getError());
} else {
    //创建成功
}

使用系统自带封装服务

use app\services\other\UploadService;

//根据系统设置自动实例化上传类
$upload = UploadService::init();
//执行上传
$path = '';
$res = $upload->to($path)->validate()->move();

//上传失败
if (false === $res) {
    var_dump($upload->getError());
} else {
    var_dump($res->uploadInfo); //上传文件信息
    var_dump($res->realName); //文件真实名称
    var_dump($res->fileName); //文件上传后的名称
    var_dump($res->filePath); //文件上传后的路径地址
}

Was this helpful?

0 / 0

发表回复 0