推荐使用composer方式
composer方式安装SDK的步骤如下:
-
在项目的根目录运行
composer require aliyuncs/oss-sdk-PHP
,或者在composer.json
文件中添加依赖关系如下:"require": {
"aliyuncs/oss-sdk-PHP": "~2.x.x"
}
-
运行
composer install
,安装依赖。安装完成后,目录结构如下:
/*
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就是访问地址