当前位置:网站首页>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]);
}
}
边栏推荐
- Wechat forum exchange applet system graduation design completion (6) opening defense ppt
- Dynamics 365 查找字段过滤
- Binary tree
- JMeter interface automated test read case, execute and write back result
- USB(十四)2022-04-12
- 网络安全-钓鱼
- Bit operation
- Bea-3xxxxx error code
- 13、 System optimization
- Technology at home and abroad people "see" the future of audio and video technology
猜你喜欢
Description of longitude and latitude PLT file format
Install a new version of idea. Double click it to open it
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
iNFTnews | NFT技术的广泛应用及其存在的问题
十四、数据库的导出和导入的两种方法
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
Wechat forum exchange applet system graduation design completion (6) opening defense ppt
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
二叉树(Binary Tree)
随机推荐
Add data analysis tools in Excel
Statistical method for anomaly detection
【编译原理】词法分析设计实现
微信论坛交流小程序系统毕业设计毕设(5)任务书
./ setup. Insufficient sh permission
Ros2 topic (03): the difference between ros1 and ros2 [01]
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
微信论坛交流小程序系统毕业设计毕设(3)后台功能
网格(Grid)
Locate to the bottom [easy to understand]
网络安全-对操作系统进行信息查询
Solution: prompt "unsupported video format" when inserting avi format video into the message
[microservices SCG] gateway integration Sentinel
聊聊支付流程的设计与实现逻辑
网络安全-CSRF
LeeCode -- 6. Zigzag transformation
Network security CSRF
opencv scalar传入三个参数只能显示黑白灰问题解决
USB(十六)2022-04-28
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题