thinkphp5结合阿里oss 多图上传,刚刚完成 最新的 ,哈哈,咱们来看一下。php
首先就是下载阿里云的oss包了,我是用composer下载的,下载命令是html
composer require aliyuncs/oss-sdk-php 执行完 等着 就行,完事以后会在vendor下生成阿里云的包,以下图所示:thinkphp
而后 你要准备你的oss一些账号 ,须要四个东西把 大概 浏览器
分别是微信
$accessKeyId, $accessKeySecret, $endpoint,$bucket.
其中前2个是自动生成的,第三个也是现成的 就是一个 网络地址 例如
http://oss-cn-hangzhou.aliyuncs.com
第四个bucket须要你新建一个bucket,而后本身命名,命名好了 拿来就能用了,都完事了就开发把。
首先是common.php,存的是调用阿里oss的公共方法,以下图
1 <?php 2 namespace app\index\controller; 3 4 use think\Controller; 5 use think\Config; 6 use OSS\OssClient; 7 use OSS\Core\OssException; 8 class common extends Controller 9 { 10 Public function moveOss($accessKeyId,$accessKeySecret,$endpoint,$bucket,$object,$content) 11 { 12 try { 13 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); 14 $res= $ossClient->putObject($bucket, $object, $content); 15 } catch (OssException $e) { 16 print $e->getMessage(); 17 } 18 return $res['info']['url']; 19 } 20 }
而后新建一个index.php来继承common.php网络
<?php namespace app\index\controller; use think\Controller; use think\File; class Index extends common { public function index() { error_reporting(0); header("Content-type:text/html;charset=utf-8"); if($this->request->isPost()){ $arrList1= $_FILES['image']['name']; $arrList2= $_FILES['image']['tmp_name']; $info2=array(); for($i=0;$i<count($arrList1);$i++){ $object= $arrList1[$i]; $content=file_get_contents($arrList2[$i]); $info=$this->moveOss('LTAIGJHbVAIejTF9','shSZbjwZVz3OvAWMPESVFqrDO2TpYo', 'http://oss-cn-hangzhou.aliyuncs.com','guanlutu',$object,$content); $arr2[]=$info; //echo $info;echo "<br/>"; } $result=implode(';',$arr2); print_r($result); }else{ return view(); } } }
最后一步就是 view页面了 view/index/index.html的代码app
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form enctype="multipart/form-data" method="post" name="fileinfo" action="{:url('index/index')}"> <table> <tr> <td>上传文件:</td> <td><input type="file" name="image[]" multiple="multiple"></td> </tr> <tr> <td colspan="2"><input type="submit" value="上传" ></td> </tr> </table> </form> </body> </html>
其中要说的是 composer
multiple="multiple" 这个属性 支持大部分pc浏览器和微信浏览器 能够直接多图上传,电脑的话按住ctrl键选择图片就能够了,就是这样,有问题群里找我把。