当前位置:网站首页>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);重要解决这个需求 了。开心
边栏推荐
- Introduction to shortcut keys in debug chapter
- 软件缺陷的管理
- flinksql client 连接kafka select * from table没有数据报错,如何解决?
- MySQL historical data supplement new data
- JS operator
- Introduction to pl/sql, very detailed notes
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- JS数据类型以及相互转换
- Attendance system based on w5500
- Maskgae: masked graph modeling meets graph autoencoders
猜你喜欢
随机推荐
W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
Esp8266 uses drv8833 drive board to drive N20 motor
已解决The JSP specification requires that an attribute name is preceded by whitespace
Return and finally? Everyone, please look over here,
toString()与new String()用法区别
Hardware peripherals =maixpy3
JS常用内置对象 数据类型的分类 传参 堆栈
W5500 is in TCP_ In server mode, you cannot Ping or communicate in the switch / router network.
W5500多节点连接
brpc源码解析(三)—— 请求其他服务器以及往socket写数据的机制
Solved files' name is invalid or doors not exist (1205)
SQL language (6)
Various controls ==pyqt5
【mysql学习09】
如何解决“W5500芯片在TCP_Client模式下,断电重启之后无法立即连接到服务器”的问题
【mysql学习08】
教你如何通过MCU配置S2E为TCP Client的工作模式
Small program of vegetable distribution in community
Reflection reflection
【leetcode刷题】


![[MySQL learning 09]](/img/27/2578f320789ed32552d6f69f14a151.png)





