当前位置:网站首页>php 一台服务器传图片到另一台上 curl post file_get_contents保存图片
php 一台服务器传图片到另一台上 curl post file_get_contents保存图片
2022-07-25 11:02:00 【hexiaoniao】
在做小程序 上传图片到B服务器。但是B服务器上协议是http的。所以有个中转服务器A
小程序上传图片->A服务器->B服务器
网上找的答案太水了,试了都不成功了。也是很无语了。
小程序里上传图片chooseImage
wx.uploadFile({
url: app.globalData.baseUrl + 'uploadUseravatar', //A服务器接口
filePath: tempFilePaths[0],//图片路径
name: 'file',
formData: {
uid: uid,//其他参数
openid: openid,//其他参数
link: link //其他参数
},
header: {
"Content-Type": "multipart/form-data"
},
A服务器接收图片:
$request = new Request();
$file = $request->file('file');$info = $file->move(ROOT_PATH . 'uploads/avatar',"file.png");//将图片保存在服务器根下/uoloads/avatar文件夹下
$filename=$info->getSaveName();//文件名
$path= 'https:/AA.cn/uploads/avatar/'.$filename;//图片在A服务器上的访问路径
$geturl="http://BB.CN/rest?method=base.user.saveAvatar";//B服务器上接收图片接口
$post_data = array(
"uid" => $uid,
"upload" => $path,//文件路径
"filename"=>$filename,文件名称
);
header("Access-Control-Allow-Origin: *");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $geturl);
curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据
curl_setopt($curl, CURLOPT_TIMEOUT, 20);//超时设置
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
B服务器接收图片
$filepath=input('upload','');//图片绝对路径
$filename=input('filename','');//图片名称
$model = model('edu/Student');
$savepath = "./";B服务器上保存路径
$mao=file_get_contents($filepath);
$res=file_put_contents($savepath,$mao);重要解决这个需求 了。开心
边栏推荐
- 【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
- 2022 年中回顾|一文看懂预训练模型最新进展
- W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
- Esp8266 uses drv8833 drive board to drive N20 motor
- Classification parameter stack of JS common built-in object data types
- W5500上传温湿度到oneNET平台
- 教你如何通过MCU将S2E配置为UDP的工作模式
- Teach you how to configure S2E as the working mode of TCP server through MCU
- JS scope and pre parsing
- JS作用域以及预解析
猜你喜欢

Linked list related (design linked list and ring linked list)

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network

Javescript loop

Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.

W5500 upload temperature and humidity to onenet platform

Brpc source code analysis (VIII) -- detailed explanation of the basic class eventdispatcher

Brpc source code analysis (VII) -- worker bthread scheduling based on parkinglot

Small program of vegetable distribution in community

Differences in usage between tostring() and new string()

brpc源码解析(七)—— worker基于ParkingLot的bthread调度
随机推荐
JS常用内置对象 数据类型的分类 传参 堆栈
【MySQL 17】安装异常:Could not open file ‘/var/log/mysql/mysqld.log‘ for error logging: Permission denied
2022 年中回顾|一文看懂预训练模型最新进展
菜单栏+状态栏+工具栏==PYQT5
Greedy problem 01_ Activity arrangement problem
[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel
Web APIs (get element event basic operation element)
varest蓝图设置json
Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
软件测试阶段的风险
Management of software defects
Greedy problem 01_ Activity arrangement code analysis
JaveScript循环
brpc源码解析(六)—— 基础类socket详解
JS中的对象
Plot ==pyqt5
第4章线性方程组
Layout management ==pyqt5
Filter过滤器解决request请求参数乱码的原理解析
【leetcode刷题】