PHP curl put方式上传文件
2024-08-26 11:05:10
发送端:
<?php function curlPut($destUrl, $sourceFileDir, $headerArr = array(), $timeout = )
{
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而不直接输出
curl_setopt($ch, CURLOPT_URL, $destUrl); //设置put到的url
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证对等证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ); //不检查服务器SSL证书 curl_setopt($ch, CURLOPT_PUT, true); //设置为PUT请求
curl_setopt($ch, CURLOPT_INFILE, fopen($sourceFileDir, 'rb')); //设置资源句柄
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($sourceFileDir)); $response = curl_exec($ch);
if ($error = curl_error($ch))
{
$bkArr = array(
'code' => ,
'msg' => $error,
);
}
else
{
$bkArr = array(
'code' => ,
'msg' => 'ok',
'resp' => $response,
);
} curl_close($ch); // 关闭 cURL 释放资源 return $bkArr;
} $destUrl = 'http://www.songjm.com/http_put_save.php';
$sourceFileDir = 'asset/pic.png';
$headerArr = array(
'filename:newname.png',
); $bkJson = curlPut($destUrl, $sourceFileDir, $headerArr);
$bkArr = json_decode($bkJson, true);
echo "<pre>";
print_r($bkArr);
die;
接收端:
<?php if ($_SERVER['REQUEST_METHOD'] != 'PUT')
{
$bkMsg = array(
'code' => -,
'msg' => 'not put',
);
echo json_encode($bkMsg);
exit();
} $filename = $_SERVER['HTTP_FILENAME']; $fileSaveDir = 'upload/';
$newFile = $fileSaveDir.$filename; $handleToSave = fopen($newFile,'wb+');
$handleSource = fopen('php://input','rb'); while (!feof($handleSource))
{
fwrite($handleToSave, fread($handleSource, ));
} fclose($handleToSave);
fclose($handleSource); $bkMsg = array(
'code' => ,
'msg' => 'ok',
);
echo json_encode($bkMsg);
exit();
转自 https://www.cnblogs.com/songjianming/archive/2019/06/23/11072958.html
最新文章
- GestureDetectorl监听
- IOS - Create Push Segue Animation Without UINavigationController
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
- linux上使用amoeba实现MySql集群,以及读写分离,主从复制
- GPIO初始化之PB3/PB4/PA13/PA14/PA15引脚的复用--寄存器版本
- Eclipse : cannot open output file(c++)
- WCF序列化与反序列化问题
- SimpleDateFormat的线程安全问题
- 浅析 JavaScript 组件编写
- hdu1881(贪心+dp)
- drozer使用
- 【node】使用nvm管理node版本
- CSS属性速查表
- PHP开发——超全局数组变量
- Hadoop -- ES -- CURD
- Java零基础教程(二)基础语法
- django版本切换以及更改url(pycharm)
- New动态分配 Delete 释放内存
- android run/debug configurations时报错Cannot reload AVD list:
- CentOS7 更换OpenStack-queens源