1.首先我们要下载阿里云oss的sdk包;(可以下载原版的,改过的通用版在本人的百度云,嘎嘎嘎~)
2.下载好之后放到项目文件目录里面
3.要在需要的控制器引用这个sdk文件例如:
include("./ThinkPHP/Library/Vendor/oss/index.php");
然后实例化这个类,调用上传的方法
===================================================
再看类文件:下载好了以后修改类文件的index.php
<?php
use OSS\OssClient;
use OSS\Core\OssException;
require_once __DIR__ . '/autoload.php';
class Oss{
function upload($object,$file){
$accessKeyId = "<你的阿里云上面的accessKeyId>";
$accessKeySecret = "<你的阿里云上面的accessKeySecret>";//一般通过短信方式获取
$endpoint = "peita.oss-cn-";//你的外网域名
$bucket = 'peita';//生成好的bucket
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint,true);
//判断存储空间是否存在
$isExist = $ossClient->doesBucketExist($bucket);
if(!$isExist)
{
throw new OssException('bucket is not exist');
}
// $object = "images/IMG_0096.JPG"; //oss 存储文件路径
// $file = './Public/images/header_left.jpg'; //本地文件路径
$options = array();
$ossClient->uploadFile($bucket, $object, $file, $options);
//$result = $ossClient->multiuploadFile($bucket, $object, $filePath);
} catch (OssException $e) {
print $e->getMessage();
}
}
}