curl put方式上传文件
程序员文章站
2022-10-24 16:45:19
发送端 接收端 ......
发送端
1 <?php 2 3 function curlput($desturl, $sourcefiledir, $headerarr = array(), $timeout = 10) 4 { 5 $ch = curl_init(); //初始化curl 6 curl_setopt($ch, curlopt_returntransfer, true); //返回字符串,而不直接输出 7 curl_setopt($ch, curlopt_url, $desturl); //设置put到的url 8 curl_setopt($ch, curlopt_httpheader, $headerarr); 9 curl_setopt($ch, curlopt_timeout, $timeout); 10 curl_setopt($ch, curlopt_ssl_verifypeer, false); //不验证对等证书 11 curl_setopt($ch, curlopt_ssl_verifyhost, 0); //不检查服务器ssl证书 12 13 curl_setopt($ch, curlopt_put, true); //设置为put请求 14 curl_setopt($ch, curlopt_infile, fopen($sourcefiledir, 'rb')); //设置资源句柄 15 curl_setopt($ch, curlopt_infilesize, filesize($sourcefiledir)); 16 17 $response = curl_exec($ch); 18 if ($error = curl_error($ch)) 19 { 20 $bkarr = array( 21 'code' => 0, 22 'msg' => $error, 23 ); 24 } 25 else 26 { 27 $bkarr = array( 28 'code' => 1, 29 'msg' => 'ok', 30 'resp' => $response, 31 ); 32 } 33 34 curl_close($ch); // 关闭 curl 释放资源 35 36 return $bkarr; 37 } 38 39 $desturl = 'http://www.songjm.com/http_put_save.php'; 40 $sourcefiledir = 'asset/pic.png'; 41 $headerarr = array( 42 'filename:newname.png', 43 ); 44 45 $bkjson = curlput($desturl, $sourcefiledir, $headerarr); 46 $bkarr = json_decode($bkjson, true); 47 echo "<pre>"; 48 print_r($bkarr); 49 die;
接收端
1 <?php 2 3 if ($_server['request_method'] != 'put') 4 { 5 $bkmsg = array( 6 'code' => -1, 7 'msg' => 'not put', 8 ); 9 echo json_encode($bkmsg); 10 exit(); 11 } 12 13 $filename = $_server['http_filename']; 14 15 $filesavedir = 'upload/'; 16 $newfile = $filesavedir.$filename; 17 18 $handletosave = fopen($newfile,'wb+'); 19 $handlesource = fopen('php://input','rb'); 20 21 while (!feof($handlesource)) 22 { 23 fwrite($handletosave, fread($handlesource, 1024)); 24 } 25 26 fclose($handletosave); 27 fclose($handlesource); 28 29 $bkmsg = array( 30 'code' => 1, 31 'msg' => 'ok', 32 ); 33 echo json_encode($bkmsg); 34 exit();
推荐阅读
-
PHP使用curl请求实现post方式上传图片文件功能示例
-
PHP5.0~5.6 各版本兼容性cURL文件上传功能实例分析
-
可兼容php5与php7的cURL文件上传功能实例分析
-
上传大文件-断点续传的一中方式的记录
-
文件上传下载,命令之wget / curl / which / sort / uniq / cut / wc
-
Ajax方式上传文件报错"Uncaught TypeError: Illegal invocation"
-
PHP实现通过CURL上传文件功能示例
-
通过Ajax方式上传文件使用FormData进行Ajax请求
-
Django中的文件的上传的几种方式
-
详解Ajax和form+iframe 实现文件上传的方法(两种方式)