先用composer切换到项目根目录执行
composer require aliyuncs/oss-sdk-php
安装好了后如图
模块创建aliyunoss,并创建相应的控制器视图文件夹,在aliyunoss文件夹下创建common配置文件
<?php
namespace app\aliyunoss\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'];
}
}
创建视图:
<!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>
控制器文件:
<?php
namespace app\aliyunoss\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('你的LTAIwoPLgOMBFbjY','你的accessKeySecret','填写你的地址','你的bucket',$object,$content);
$arr2[]=$info;
//echo $info;echo "<br/>";
}
$result=implode(';',$arr2);
print_r($result);
}else{
return view();
}
}
}
上传成功后返回地址: