推荐使用composer方式
composer方式安装SDK的步骤如下:
在项目的根目录运行composer require aliyuncs/oss-sdk-php,或者在composer.json文件中添加依赖关系如下:
"require": {
"aliyuncs/oss-sdk-php": "~2.x.x"
}
运行composer install,安装依赖。安装完成后,目录结构如下:
.
├── app.php
├── composer.json
├── composer.lock
└── vendor
其中index.php是您的应用程序,vendor/目录下包含了所依赖的库。您需要在app.php中添加依赖关系如下:
require_once __DIR__ . '/vendor/autoload.php';
后台代码:
/* author:咔咔 wechat:fangkangfk address:陕西西安 */ <?php namespace app\miniapp\controller\upload; use app\miniapp\controller\Common; use think\Image; use think\Controller; use think\Request; /**/ use OSS\OssClient; use OSS\Core\OssException; class Upload extends Common { public function index(){ $scr = $_FILES['file']['tmp_name']; $ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀 $dst = md5(time()).'-'.$scr.'.'.$ext; //上传文件名称 // $this->load->library('AliUpload'); $url = $this->upload($dst,$scr); $data = array('url' =>$url); } public function upload($dst,$src){ $accessKeyId = "ID"; $accessKeySecret = "秘钥"; // Endpoint以杭州为例,其它Region请按实际情况填写。 $endpoint = "节点"; $bucket = '存储空间名'; @error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING); //获取对象 $auth = new OssClient($accessKeyId,$accessKeySecret,$endpoint); try { //上传图片 $result = $auth->uploadFile($bucket,$dst,$src); dump($result);die; return $result['info']['url']; } catch (OssException $e) { return $e->getMessage(); } } }
上传成功后会返回:info.url就是访问地址