当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP使用curl模拟post上传及接收文件的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-13 10:31:43 浏览: 评论:0 

这篇文章主要介绍了PHP使用curl模拟post上传及接收文件的方法,涉及PHP使用curl实现post文件传输的相关技巧,需要的朋友可以参考下。

本文实例讲述了PHP使用curl模拟post上传及接收文件的方法,分享给大家供大家参考,具体如下:

  1. public function Action_Upload(){ 
  2.      $this->path_config(); 
  3.     exit(); 
  4.     $furl="@d:\develop\JMFrameworkWithDemo.rar"; 
  5.     $url= "http://localhost/DemoIndex/curl_pos/"; 
  6.     $this->upload_file_to_cdn($furl, $url); 
  7. } 
  8. public function upload_file_to_cdn($furl,$url){ 
  9.     //  初始化 
  10.     $ch = curl_init(); 
  11.     // 要上传的本地文件地址"@F:/xampp/php/php.ini"上传时候,上传路径前面要有@符号 
  12.     $post_data = array ( 
  13.       "upload" => $furl 
  14.     ); 
  15.     //print_r($post_data); 
  16.     //CURLOPT_URL 是指提交到哪里?相当于表单里的“action”指定的路径 
  17.     //$url = "http://localhost/DemoIndex/curl_pos/"; 
  18.     //  设置变量 
  19.     curl_setopt($ch, CURLOPT_URL, $url); 
  20.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//执行结果是否被返回,0是返回,1是不返回 
  21.     curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示 
  22.     //伪造网页来源地址,伪造来自百度的表单提交 
  23.     curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com"); 
  24.     //表单数据,是正规的表单设置值为非0 
  25.     curl_setopt($ch, CURLOPT_POST, 1); 
  26.     curl_setopt($ch, CURLOPT_TIMEOUT, 100);//设置curl执行超时时间最大是多少 
  27.     //使用数组提供post数据时,CURL组件大概是为了兼容@filename这种上传文件的写法, 
  28.     //默认把content_type设为了multipart/form-data。虽然对于大多数web服务器并 
  29.     //没有影响,但是还是有少部分服务器不兼容。本文得出的结论是,在没有需要上传文件的 
  30.     //情况下,尽量对post提交的数据进行http_build_query,然后发送出去,能实现更好的兼容性,更小的请求数据包。 
  31.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
  32.     //   执行并获取结果 
  33.     curl_exec($ch); 
  34.     if(curl_exec($ch) === FALSE) 
  35.     { 
  36.       echo "<br/>"," cUrl Error:".curl_error($ch); 
  37.     } 
  38.     //  释放cURL句柄 
  39.     curl_close($ch); 
  40.     echo "aaa45"; 
  41. } 
  42. function action_curl_pos(){ 
  43.     var_dump($_FILES); 
  44.     $aa= move_uploaded_file($_FILES["upload"]["tmp_name"], "/wamp/tools/1.rar"); 
  45.     if($aa){ 
  46.       echo "11"; 
  47.     } 
  48. }

Tags: PHP上传 curl上传

分享到: