当前位置:网站首页>PHP uses Alibaba cloud storage
PHP uses Alibaba cloud storage
2022-07-07 23:21:00 【Code is a bug】
Alibaba cloud object storage website
Alibaba cloud object storage website
Install alicloud sdk
Use composer Install or use other methods according to the official website
composer require aliyuncs/oss-sdk-php
Upload files
<?php
use OSS\OssClient;
use OSS\Core\OssException;
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
class OssUpload
{
public function upload($file)
{
// Alicloud account AccessKey Have all the API Access rights of , The risk is high . It is highly recommended that you create and use it RAM The user carries out API Visit or daily operations , Please log in RAM Console creation RAM user .
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// yourEndpoint Fill in Bucket The region corresponding to Endpoint. East China 1( Hangzhou ) For example ,Endpoint Fill in for https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Fill in Bucket name , for example examplebucket.
$bucket= "examplebucket";
// Fill in Object The full path , for example exampledir/exampleobject.txt.Object The full path cannot contain Bucket name .
$object = "uploads/".date('Y',time()).date('m',time()).date('d',time())."/".$file['name']; // Storage path of files in Alibaba cloud
// <yourLocalFile> By the local file path plus file name including suffixes , for example /users/local/myfile.txt.
// Fill in the full path of the local file , for example D:\\localpath\\examplefile.txt. If no local path is specified , By default, the file is uploaded from the local path corresponding to the project to which the sample program belongs .
$filePath =$file['tmp_name']; // File path
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->uploadFile($bucket, $object, $filePath);
} catch
(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
// print(__FUNCTION__ . "OK" . "\n");
return $object; // Upload successful , Return file path
}
}
<?php
namespace app\app\controller;
use app\app\model\AppLandpoint;
use app\common\controller\Api;
use app\common\controller\OssUpload;
class LandPoint
{
/* * @description Photo upload */
public function ImgUpload() {
$file=$_FILES['img'];
$upload=new OssUpload();
$data=$upload->upload($file);
return json(['code','data'=>$data]);
}
}
边栏推荐
- Technology at home and abroad people "see" the future of audio and video technology
- iNFTnews | NFT技术的广泛应用及其存在的问题
- 微信论坛交流小程序系统毕业设计毕设(4)开题报告
- kubernetes的简单化数据存储StorageClass(建立和删除以及初步使用)
- 网络安全-安装CentOS
- 微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
- ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
- 聊聊支付流程的设计与实现逻辑
- In the field of software engineering, we have been doing scientific research for ten years!
- USB(十六)2022-04-28
猜你喜欢
随机推荐
About idea cannot find or load the main class
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
ArcGIS:矢量要素相同字段属性融合的两种方法
Network security CSRF
[microservices SCG] gateway integration Sentinel
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
Redhat下安装fedora
js 获取对象的key和value
网络安全-联合查询注入
Classification and prediction of heartbeat signal
1. Sum of two numbers
违法行为分析1
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
leetcode-520. 检测大写字母-js
Wechat forum exchange applet system graduation design completion (1) development outline
二叉树(Binary Tree)
Vs extension tool notes
为什么市场需要低代码?
14、 Two methods of database export and import