61 lines
2.4 KiB
PHP
61 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace alioss;
|
|
|
|
use OSS\Core\OssException;
|
|
use OSS\OssClient;
|
|
|
|
class alioss
|
|
{
|
|
private string $accessKeyId = 'LTAI5tRFwjTfzjLRuZkyjbWe';
|
|
private string $accessKeySecret = 'SPseMZwcEGIhm7cg0JneE9lb3CAB82';
|
|
private string $endpoint = 'oss-cn-chengdu.aliyuncs.com';
|
|
private string $bucket = 'lihaiim';
|
|
|
|
public function uploadImg($filePath,$fileType,$fileSize): array
|
|
{
|
|
//验证图片类型
|
|
$ext = ['jpg','jpeg','png','gif'];
|
|
if(!in_array($fileType,$ext)){
|
|
return ['code'=>0,'msg'=>'图片格式错误'];
|
|
}
|
|
try {
|
|
//实例化对象 将配置传入
|
|
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
|
|
//这里是有sha1加密 生成文件名 之后连接上后缀
|
|
$fileName = sha1(date('YmdHis', time()) . uniqid()) . '.' . $fileType;
|
|
//上传至阿里云的目录 为年+月/日的格式
|
|
$pathName = date('Y-m/d') . '/' .$fileName;
|
|
//执行阿里云上传 bucket名称,上传的目录,文件
|
|
$result = $ossClient->uploadFile($this->bucket, $pathName, $filePath);
|
|
} catch (OssException $e) {
|
|
return ['code'=>0,'msg'=>$e->getMessage()];
|
|
}
|
|
//将结果输出
|
|
return ['code'=>1,'msg'=>'上传成功','data'=>$result['info']['url']];
|
|
}
|
|
|
|
public function uploadVideo($filePath,$fileType,$fileSize): array
|
|
{
|
|
//验证视频类型
|
|
$ext = ['mp4','avi','flv','wmv','swf'];
|
|
if(!in_array($fileType,$ext)){
|
|
return ['code'=>0,'msg'=>'视频格式错误'];
|
|
}
|
|
try {
|
|
//实例化对象 将配置传入
|
|
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
|
|
//这里是有sha1加密 生成文件名 之后连接上后缀
|
|
$fileName = sha1(date('YmdHis', time()) . uniqid()) . '.' . $fileType;
|
|
//上传至阿里云的目录 为年+月/日的格式
|
|
$pathName = date('Y-m/d') . '/' .$fileName;
|
|
//执行阿里云上传 bucket名称,上传的目录,文件
|
|
$result = $ossClient->uploadFile($this->bucket, $pathName, $filePath);
|
|
} catch (OssException $e) {
|
|
return ['code'=>0,'msg'=>$e->getMessage()];
|
|
}
|
|
//将结果输出
|
|
return ['code'=>1,'msg'=>'上传成功','data'=>$result['info']['url']];
|
|
}
|
|
|
|
} |