您现于de位置乃:亚博 > APP 开发

亚博 2019-07-11 APP 开发 396

app开发5.0整合阿里云OSS开放存储云服务

第1步:执行composer

composer require aliyuncs/oss-sdk-app

 执行完毕可于vender下看到组件


 


第2步:预先准备de     $accessKeyId, $accessKeySecret, $endpoint $bucket.


第3步:于 application/index/controller/Common.app 创建Common.app  存阿里云ossde公共方法


<?app
namespace app\index\controller;
 
use think\Controller;
use think\Config;
use OSS\OssClient;
use OSS\Core\OssException;
class common extends Controller
{
    Public function moveOss($accessKeyId,$accessKeySecret,$endpoint,$bucket,$object,$content)
    {
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $res= $ossClient->putObject($bucket, $object, $content);
        } catch (OssException $e) {
            print $e->getMessage();
        }
        return $res['info']['url'];
    }
}


第4步:于 application/index/controller/Index.app 创建Index.app  继承以上de Common

<?app
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('AccessKey ID','Access Key Secret','EndPoint(地域节点)','Bucket名',$object,$content);
                $arr2[]=$info;
              //echo $info;echo "<br/>";
            }
           $result=implode(';',$arr2);
           print_r($result);
 
        }else{
            return view();
        }
 
    }
  
}


第5步:视图demo 1个 view/index/index.html 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>阿里云oss文件上传</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="fileinfo" action="{:url(/html//'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>


评论