当前位置:网站首页>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);重要解决这个需求 了。开心
边栏推荐
- 相似矩阵,可对角化条件
- Brpc source code analysis (VI) -- detailed explanation of basic socket
- 【leetcode刷题】
- W5500 adjusts the brightness of LED light band through upper computer control
- 第4章线性方程组
- JS常用内置对象 数据类型的分类 传参 堆栈
- Information management system for typical works of urban sculpture (picture sharing system SSM)
- JS中的函数
- 油猴脚本链接
- 谣言检测文献阅读十一—Preventing rumor spread with deep learning
猜你喜欢
![[electronic device notes 5] diode parameters and selection](/img/4d/05c60641dbdbfbfa6c3cc19a24fa03.png)
[electronic device notes 5] diode parameters and selection

Leetcode sword finger offer 28. symmetric binary tree

模型部署简述

Teach you how to configure S2E to UDP working mode through MCU

Learn NLP with Transformer (Chapter 1)

Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees

"Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities

JS中的函数

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

任何时间,任何地点,超级侦探,认真办案!
随机推荐
[USB device design] - composite device, dual hid high-speed (64BYTE and 1024byte)
Plot ==pyqt5
Oracle parsing XML with the same name
Dynamic planning problem 03_ Maximum sub segment sum
Maskgae: masked graph modeling meets graph autoencoders
模型部署简述
JS流程控制
Dynamic planning question 05_ Missile interception
Reinforcement learning (IV)
Varest blueprint settings JSON
"Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities
2022 年中回顾|一文看懂预训练模型最新进展
brpc源码解析(七)—— worker基于ParkingLot的bthread调度
What is the global event bus?
【mysql学习08】
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
教你如何通过MCU将S2E配置为UDP的工作模式
W5500上传温湿度到oneNET平台
Classification parameter stack of JS common built-in object data types
JS常用内置对象 数据类型的分类 传参 堆栈