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. CF949D Curfew

    传送门 跟这个大佬学的->戳我 假设只有一个宿管,那么从前往后做的过程中,如果能到达某个寝室范围内的人数不够\(b\),那么不如把这个寝室空出来,这样更有利于后面的抉择;反之,就把这个寝室搞正好 ...

  2. NSOperation 代码,阐述NSOperation一般功能和重要功能

    // // ViewController.m // 05-NSOperation // // Created by jerry on 15/9/5. // Copyright (c) 2015年 je ...

  3. SpringBoot集成SpringCloud

    (1).新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等. 项目结构: 公共Bean: package cn.coreqi.entities; import java.io.Se ...

  4. python计算最大公约数和最小公倍数

    a=4 b=2 def gcd(a,b): return a if b==0 else gcd(b,a%b) def lcm(a,b): return a*b//gcd(a,b) print(gcd( ...

  5. shell编程之helloworld

    /bin/sh与/bin/bash的区别sh:如果前面有语句报错,则报错语句后面的命令不执行bash:如果前面有语句报错,后面的命令也会执行sh跟bash的区别,实际上就是bash有没有开启posix ...

  6. oem 重建

    OracleDBControl启动失败to local from URL=http://your-url.co     方法: emca -deconfig dbcontrol db -repos d ...

  7. 常见的移动端Web页面问题

    移动端Web需要照顾触摸操作的体验,以及更多的屏幕旋转与尺寸适配等问题,非常琐碎,在这里为大家倾力总结多条常见的移动端Web页面问题解决方案,欢迎收看收藏! 1.安卓浏览器看背景图片,有些设备会模糊 ...

  8. androidpn 推送系统

    (文中部分内容来自网络,如无意中侵犯了版权,请告之!) XMPP协议: XMPP : The Extensible Messaging andPresence Protocol. 中文全称:可扩展通讯 ...

  9. 5个php实例,细致说明传值与传引用的区别

    传值:是把实参的值赋值给行参 ,那么对行参的修改,不会影响实参的值 传引用 :真正的以地址的方式传递参数传递以后,行参和实参都是同一个对象,只是他们名字不同而已对行参的修改将影响实参的值 说明: 传值 ...

  10. poj2342 没有上司的舞会 树形dp基础

    #include<iostream> #include<cstring> #include<cstdio> #include<vector> using ...