1.先用composer安装阿里云OSS的PHPSDK

阿里云OSS文件上传封装-LMLPHP

2.配置文件里定义阿里云OSS的秘钥

阿里云OSS文件上传封装-LMLPHP

3.在index控制器里的代码封装

<?php

namespace app\index\controller;

use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Core\OssUtil;
use think\Config; class Index extends Base
{
// 阿里OSS相关参数
protected $accessKeyId = '';
protected $accessKeySecret = '';
protected $endpoint = '';
protected $bucket = '';
// 文件上传相关设置
protected $image_size = ;
protected $video_size = ;
protected $other_size = ; /**
* 构造函数
*/
public function _initialize()
{
$this->accessKeyId = Config::get('aliyun_oss')['accessKeyId'];
$this->accessKeySecret = Config::get('aliyun_oss')['accessKeySecret'];
$this->endpoint = Config::get('aliyun_oss')['endpoint'];
$this->bucket = Config::get('aliyun_oss')['bucket'];
$this->image_size = Config::get('upload_set')['image_size'];
$this->video_size = Config::get('upload_set')['video_size'];
$this->other_size = Config::get('upload_set')['other_size'];
} /**
* 测试页面
*/
public function index()
{
return $this->fetch();
} /**
* 创建存储空间
*/
public function createBucket()
{
if (!request()->isPost()) {
throw new \think\Exception('请求方式错误!');
}
$bucket = input('param.bucket');
if (empty($bucket)) {
return json(['data' => '', 'code' => , 'message' => '存储空间名不能为空!']);
}
try {
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$ossClient->createBucket($bucket);
return json(['data' => '', 'code' => , 'message' => $bucket . '存储空间创建成功']);
} catch (OssException $e) {
return json(['data' => $e->getMessage(), 'code' => , 'message' => '创建失败']);
}
} /**
* 上传文件
*/
public function uploadFile()
{
/*判断提交方式*/
if (!request()->isPost()) {
throw new \think\Exception('请求方式错误!');
}
/*获取到上传的文件*/
$file = $_FILES['file'];
if (!$file) {
return json(['data' => '', 'code' => , 'message' => '文件不存在!']);
}
// 判断文件大小
if ($file['size'] > $this->other_size) {
return json(['data' => '', 'code' => , 'message' => '文件大小不能超过' . ($this->other_size / / ) . 'M']);
}
$name = $file['name'];
$format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
/*判断图片格式*/
$allow_type = ['.zip', '.rar', '.doc','.docx','xls','xlsx','mp3','wav'];
if (!in_array($format, $allow_type)) {
return json(['data' => '', 'code' => , 'message' => '文件格式不在允许范围内']);
}
// 尝试执行
try {
//实例化对象 将配置传入
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
//这里是有sha1加密 生成文件名 之后连接上后缀
$fileName = 'upload/file/' . date("Ymd") . '/' . sha1(date('YmdHis', time()) . uniqid()) . $format;
//执行阿里云上传
$result = $ossClient->uploadFile($this->bucket, $fileName, $file['tmp_name']);
/*组合返回数据*/
$arr = [
'oss_url' => $result['info']['url'], //上传资源地址
'relative_path' => $fileName //数据库保存名称(相对路径)
];
} catch (OssException $e) {
return json(['data' => $e->getMessage(), 'code' => , 'message' => '上传失败!']);
}
//将结果返回
return json(['data' => array('file' => $arr['oss_url']), 'code' => , 'message' => '成功上传到oss']);
} /**
* 上传视频
*/
public function uploadVideo()
{
/*判断提交方式*/
if (!request()->isPost()) {
throw new \think\Exception('请求方式错误!');
}
/*获取到上传的文件*/
$file = $_FILES['file'];
if (!$file) {
return json(['data' => '', 'code' => , 'message' => '文件不存在!']);
}
// 判断文件大小
if ($file['size'] > $this->video_size) {
return json(['data' => '', 'code' => , 'message' => '视频大小不能超过' . ($this->video_size / / ) . 'M']);
}
$name = $file['name'];
$format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
/*判断图片格式*/
$allow_type = ['.mp4', '.avi', '.rmvb'];
if (!in_array($format, $allow_type)) {
return json(['data' => '', 'code' => , 'message' => '视频格式不在允许范围内']);
}
// 尝试执行
try {
//实例化对象 将配置传入
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
//这里是有sha1加密 生成文件名 之后连接上后缀
$fileName = 'upload/video/' . date("Ymd") . '/' . sha1(date('YmdHis', time()) . uniqid()) . $format;
//执行阿里云上传
$result = $ossClient->uploadFile($this->bucket, $fileName, $file['tmp_name']);
/*组合返回数据*/
$arr = [
'oss_url' => $result['info']['url'], //上传资源地址
'relative_path' => $fileName //数据库保存名称(相对路径)
];
} catch (OssException $e) {
return json(['data' => $e->getMessage(), 'code' => , 'message' => '上传失败!']);
}
//将结果返回
return json(['data' => array('file' => $arr['oss_url']), 'code' => , 'message' => '成功上传到oss']);
} /**
* 上传图片
*/
public function uploadImage()
{
/*判断提交方式*/
if (!request()->isPost()) {
throw new \think\Exception('请求方式错误!');
}
/*获取到上传的文件*/
$file = $_FILES['file'];
if (!$file) {
return json(['data' => '', 'code' => , 'message' => '文件不存在!']);
}
// 判断文件大小
if ($file['size'] > $this->image_size) {
return json(['data' => '', 'code' => , 'message' => '视频大小不能超过' . ($this->image_size / / ) . 'M']);
}
$name = $file['name'];
$format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
/*判断图片格式*/
$allow_type = ['.jpg', '.jpeg', '.gif', '.bmp', '.png'];
if (!in_array($format, $allow_type)) {
return json(['data' => '', 'code' => , 'message' => '图片格式不在允许范围内']);
}
// 尝试执行
try {
//实例化对象 将配置传入
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
//这里是有sha1加密 生成文件名 之后连接上后缀
$fileName = 'upload/image/' . date("Ymd") . '/' . sha1(date('YmdHis', time()) . uniqid()) . $format;
//执行阿里云上传
$result = $ossClient->uploadFile($this->bucket, $fileName, $file['tmp_name']);
/*组合返回数据*/
$arr = [
'oss_url' => $result['info']['url'], //上传资源地址
'relative_path' => $fileName //数据库保存名称(相对路径)
];
} catch (OssException $e) {
return json(['data' => $e->getMessage(), 'code' => , 'message' => '上传失败!']);
}
//将结果返回
return json(['data' => array('file' => $arr['oss_url']), 'code' => , 'message' => '成功上传到oss']);
} /**
* 上传图片base64
*/
public function uploadImageBase64()
{
// 判断提交方式及图片类型
if (!request()->has('base64', 'post')) {
return json(['data' => '', 'code' => , 'message' => '请求方式错误,或图片非base64格式类型']);
}
$data = $_POST['base64'];
$result = $this->new_base64_upload($data);
if ($result['code'] !== ) {
return json(['data' => '', 'code' => , 'message' => $result['msg']]);
}
$fileResult = &$result['data'];
$filePath = $fileResult['path'] . $fileResult['name'];
$ossFileName = implode('/', ['upload/image', date('Ymd'), $fileResult['name']]);
try {
//实例化对象 将配置传入
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$result = $ossClient->uploadFile($this->bucket, $ossFileName, $filePath);
$arr = [
'oss_url' => $result['info']['url'], //上传资源地址
'relative_path' => $ossFileName //数据库保存名称(相对路径)
];
} catch (OssException $e) {
return json(['data' => $e->getMessage(), 'code' => , 'message' => '上传失败']);
}
unlink($filePath);
return json(['data' => array('file' => $arr['oss_url']), 'code' => , 'message' => '成功上传到oss']);
} /**
* 将Base64数据转换成二进制并存储到指定路径
*/
protected function new_base64_upload($base64, $image_path = 'upload/posts/')
{
$data = explode(',', $base64);
trace($data, 'api');
unset($base64);
if (count($data) !== ) {
return ['code' => , 'msg' => '文件格式错误'];
}
if (!preg_match('/^(data:\s*image\/(\w+);base64)/', $data[], $result)) {
return ['code' => , 'msg' => '文件格式错误'];
}
$type = $result[];
if (!in_array($type, array('jpeg', 'jpg', 'gif', 'bmp', 'png'))) {
return ['code' => , 'msg' => '文件格式不在允许范围内'];
}
$image_name = md5(uniqid()) . '.' . $result[];
$image_file = $image_path . $image_name;
//服务器文件存储路径
try {
if (file_put_contents($image_file, base64_decode($data[]))) {
return ['code' => , 'msg' => '成功', 'data' => ['name' => $image_name, 'path' => $image_path]];
} else {
return ['code' => , 'msg' => '文件保存失败'];
}
} catch (\Exception $e) {
$msg = $e->getMessage();
return ['code' => , 'msg' => $msg];
}
}
}
04-23 15:11