1、SDK安装方法参考:https://github.com/aliyun/aliyun-oss-php-sdk/blob/master/README-CN.md
2、上代码【我是写在了 common.php 中】
| | | | 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'); | | | | $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()]); | | } | | | | } | | } | | | | return $data; | | } |
|
2、Conf目录中的oss.php内容例如:
| | | | | | | | | | return [ | | | | 'accessKeyId' => '********', | | | | 'accessKeySecret' => '********', | | | | 'endpoint' => 'http://oss-cn-********.aliyuncs.com', | | | | 'bucket' => '********', | | ]; |
|