分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

thinkphp5 结合 阿里oss进行多图上传(18年6月28日开发完成)

发布时间:2023-09-06 02:01责任编辑:傅花花关键词:thinkphp

thinkphp5结合阿里oss 多图上传,刚刚完成 最新的 ,哈哈,我们来看一下。

首先就是下载阿里云的oss包了,我是用composer下载的,下载命令是

composer require aliyuncs/oss-sdk-php  执行完 等着 就行,完事之后会在vendor下生成阿里云的包,如下图所示:

然后 你要准备你的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

<?phpnamespace 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的代码

<!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>

其中要说的是 

multiple="multiple" ?这个属性 支持大部分pc浏览器和微信浏览器 可以直接多图上传,电脑的话按住ctrl键选择图片就可以了,就是这样,有问题群里找我把。

thinkphp5 结合 阿里oss进行多图上传(18年6月28日开发完成)

原文地址:https://www.cnblogs.com/HoverM/p/9241628.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved