curl_file_create (带路径的文件名 [, 文件mimetype , 上传数据里的文件名] ) ;

new cURLFile (带路径的文件名 [, 文件mimetype , 上传数据里的文件名] ) ;

$ch = curl_init('http://example.com/upload.php');
// 创建CURLFile对象
$cfile = curl_file_create('cats.jpg','image/jpeg','test_name'); // 分配提交的数据
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
upload.php打印输出:
array(1) {
["test_file"]=>
array(5) {
["name"]=>
string(9) "test_name"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/phpPC9Kbx"
["error"]=>
int(0)
["size"]=>
int(46334)
}
}
   $ch = curl_init();
// 上传多个
$postFields = array(
'file[0]' => new cURLFile($file1, $mimetype1, $basename1),
'file[1]' => new cURLFile($file2, $mimetype2, $basename2)
) curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

将@前缀文件名转为cURLFile

if(is_array($postfields) == true)
{
foreach($postfields as $key => $value)
{
// 以@开头
if(strpos($value, '@') === 0)
{
// 得到去掉@的文件名
$filename = ltrim($value, '@');
//转为CURLFile类
$postfields[$key] = new CURLFile($filename);
}
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//上传地址
$target="http://youraddress.tld/example/upload.php";
//面向过程的方式创建CURLFile 对象
$cfile1 = curl_file_create('resource/test.png','image/png','testpic'); //面向对象 的方式创建CURLFile 对象
$cfile2 = new CURLFile('resource/test.png','image/png','testpic'); 分配post提交的数据
$imgdata =[
'myimage1' => $cfile1,
'myimage2' => $cfile2
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
//可选
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
可选
curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://someaddress.tld','Content-Type: multipart/form-data')); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 停止验证证书
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//将获取的信息以字符串返回
curl_setopt($curl, CURLOPT_POST, true); // post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); // 提交
//可选
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // 上传后有重定向
$r = curl_exec($curl);
curl_close($curl);

curl 文件上传的更多相关文章

  1. curl文件上传有两种方式,一种是post_fileds,一种是infile

    curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄! );curl_setopt($ch, CURLOPT_PO ...

  2. php curl文件上传兼容php5.0~5.6各版本

    PHP 5.0~5.6 各版本兼容的cURL文件上传 最近做的一个需求,使用PHP cURL上传文件.踩坑若干,整理如下. 不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POS ...

  3. [转]考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传

    FROM : https://segmentfault.com/a/1190000000725185 最近做的一个需求,要通过PHP调用cURL,以multipart/form-data格式上传文件. ...

  4. PHP 5.0~5.6 各版本兼容性的 cURL 文件上传

    不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POSTFIELDS传递关联数组(而不是字符串)来生成multipart/form-data的POST请求. 传统上,PHP的cU ...

  5. 考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传

    最近做的一个需求,要通过PHP调用cURL,以multipart/form-data格式上传文件.踩坑若干,够一篇文章了. 重要警告 没事不要读PHP的官方中文文档!版本跟不上坑死你! 不同版本PHP ...

  6. php curl文件上传

    <?php /** * 这是一个自动化部署的类, 非常简单,思想就是压缩,上传,然后解压覆盖,所以请小心使用. * @author liuchao <249757247@qq.com> ...

  7. 【转载】兼容php5,php7的cURL文件上传示例

    转载来自: http://www.huanlinna.com/2016/06/25/coding/php5-php7-upload-demo-via-curl.html https://segment ...

  8. php 使用curl 将文件上传

    <?php /**   *  curl文件上传   *  @var  struing  $r_file  上传文件的路劲和文件名     */ function upload_file($r_f ...

  9. 淘宝SDK扒出来的CURL调用含文件上传代码

    <?php function curl($url,$postFields=null){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); c ...

随机推荐

  1. weblogic对JSP预编译、weblogic读取JSP编译后的class文件、ant中weblogic.jspc预编译JSP

    我们都知道在weblogic中JSP是每次第一次访问的时候才会编译,这就造成第一次访问某个JSP的时候性能下降,有时候我们也希望JSP被编译成class然后打包在jar中实现隐藏JSP的功能,下面介绍 ...

  2. LOJ 2483: 洛谷 P4655: 「CEOI2017」Building Bridges

    题目传送门:LOJ #2483. 题意简述: 有 \(n\) 个数,每个数有高度 \(h_i\) 和价格 \(w_i\) 两个属性. 你可以花费 \(w_i\) 的代价移除第 \(i\) 个数(不能移 ...

  3. SpringBoot定制错误的Json数据

    (1)自定义异常处理&返回定制Json数据 @ControllerAdvice public class MyExceptionHandler { @ResponseBody @Excepti ...

  4. JavaScript中 this 的指向

    很多人都会被JavaScript中this的指向(也就是函数在调用时的调用上下文)弄晕,这里做一下总结: 首先,顶层的this指向全局对象. 函数中的this按照调用方法的不同,其指向也不同: 1.函 ...

  5. UML和模式应用5:细化阶段(8)---逻辑架构和UML包图

    1.前言 本章是从面向分析的工作过度到软件设计 典型的OO系统设计的基础是若干架构层,如UI层.应用逻辑(领域)层 本章简要考察逻辑分层架构和相关UML表示法 2.逻辑架构和层 逻辑架构 逻辑架构是软 ...

  6. ftruncate(改变文件大小)

    ftruncate(改变文件大小) 定义函数 int ftruncate(int fd,off_t length); 函数说明 ftruncate()会将参数fd指定的文件大小改为参数length指定 ...

  7. 如何在linux下检测内存泄漏(转)

    本文转自:http://www.ibm.com/developerworks/cn/linux/l-mleak/ 本文针对 linux 下的 C++ 程序的内存泄漏的检测方法及其实现进行探讨.其中包括 ...

  8. plsql developer导入导出序列方法

    导出: 1.打开PLSQL Developer,工具 2.类型排序,选中所有sequence,指定用户,单个文件,选择导出文件路径,等待执行完毕即可. 导入: 打开导出的文件,复制,在新打开的命令窗口 ...

  9. Eureka 开发时快速剔除失效服务

    Spring Cloud 版本: Dalston.SR5 服务端配置: # 关闭保护机制 eureka.server.enable-self-preservation=false #剔除失效服务间隔 ...

  10. Inno Setup 系列之安装、卸载时调用bat

    需求 想在安装的时候调用install.bat,在卸载的时候调用uninstall.bat 解决 可以这样写 Inno Setup 的脚本: [Setup] ; NOTE: The value of ...