1、SDK安装方法参考:https://github.com/aliyun/aliyun-oss-php-sdk/blob/master/README-CN.md
2、上代码【我是写在了 common.php 中】
//文件上传到oss
function fileUpload($file){
foreach ($file as $v){
$info = $v->move( './uploads','');
if($info){
$oss = config('oss.');
$accessKeyId = $oss['accessKeyId'];
$accessKeySecret = $oss['accessKeySecret'];
$endpoint = $oss['endpoint'];
$bucket= $oss['bucket'];
include_once('/aliyuncs/oss-sdk-php/autoload.php');
//include_once('aliyuncs/oss-sdk-php/autoload.php SDK自动加载文件的路径');
$ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
$filepath = './uploads/'.$info->getsaveName();
$md5 = md5_file($filepath);
//以上不用动
$result = $ossClient->uploadFile($bucket, $info->getFilename(), $filepath);
$fileId = \think\Db::name('file')->where('md5',$md5)->value('id');
if($fileId){
$data[] = $fileId;
}else{
$data[] = \think\Db::name('file')->insertGetId(['url'=>$result['info']['url'],'md5'=>$md5,'file_name'=>$info->getFilename()]);
}
}
}
// unlink($filepath);
return $data;
}
2、Conf目录中的oss.php内容例如:
// +----------------------------------------------------------------------
// | OSS相关设置
// +----------------------------------------------------------------------
return [
// accessKeyId
'accessKeyId' => '********',
// accessKeySecret
'accessKeySecret' => '********',
// endpoint
'endpoint' => 'http://oss-cn-********.aliyuncs.com',
// bucket
'bucket' => '********',
];