首先用 composer require aliyuncs/oss-sdk-php安装插件
然后在入口文件加入
require_once __DIR__ .‘/../vendor/aliyuncs/oss-sdk-php/autoload.php‘;
在文件上传类 File.php中添加一个方法
必须引入这俩文件
use OSS\OssClient;
use OSS\Core\OssException;
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‘];
}
控制器
class Img extends Controller
{
public function index()
{
return view(‘add‘);
}
public function insert()
{
$file = request()->file(‘img‘);
$img=$file->getInfo();
$object="images/".$img[‘name‘];
$content=file_get_contents($img[‘tmp_name‘]);
$info=$file->moveOss(‘LTAIYKANPIug9wAi‘,‘ZSJPmzWXdhKTss5M51UzgeOiXN3ZXD‘,
‘http://oss-cn-beijing.aliyuncs.com‘,‘yan0208‘,$object,$content);
echo $info;
}
}
composer require aliyuncs/oss-sdk-php