当前位置:网站首页>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]);
}
}
边栏推荐
- USB(十六)2022-04-28
- 位运算(Bit Operation)
- Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
- 网络安全-永恒之蓝
- Brush question 6
- Cloud native is devouring everything. How should developers deal with it?
- V20变频器手自动切换(就地远程切换)的具体方法示例
- 2021ICPC上海 H.Life is a Game Kruskal重构树
- Dynamics 365 查找字段过滤
- ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
猜你喜欢
JMeter interface automated test read case, execute and write back result
七月第一周
Brush question 3
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
LeeCode -- 6. Zigzag transformation
Mysql索引优化实战一
Unity3D学习笔记5——创建子Mesh
十三、系统优化
[microservices SCG] gateway integration Sentinel
随机推荐
What are the similarities and differences between smart communities and smart cities
微信论坛交流小程序系统毕业设计毕设(5)任务书
Clean C disk
智慧社區和智慧城市之間有什麼异同
PMP项目管理考试过关口诀-1
CXF call reports an error. Could not find conduct initiator for address:
Cloud native is devouring everything. How should developers deal with it?
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
LDO稳压芯片-内部框图及选型参数
Talk about the design and implementation logic of payment process
Dynamic agent explanation (July 16, 2020)
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
14、 Two methods of database export and import
Brush question 6
Wechat forum exchange applet system graduation design completion (1) development outline
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
turbo intruder常用脚本
iNFTnews | NFT技术的广泛应用及其存在的问题
Wechat forum exchange applet system graduation design completion (7) Interim inspection report