thinkphp5 結合 阿里oss進行多圖上傳(18年6月28日開發完成)

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鍵選擇圖片就能夠了,就是這樣,有問題羣裏找我把。
相關文章
相關標籤/搜索