ThinkPHP5.1整合文件上传到阿里云OSS的方法

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' => '********',
];
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇