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. WEB 服务器 加速缓存比较

    Nginx 相对 Apache httpd 的优点: - 轻量级,同样起web 服务,比apache 占用更少的内存及资源 - 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的 ...

  2. JavaScript之HTML5 data-* 自定义属性[HTML5标准 node.dataset.attributeName]

    在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-前缀加上自定义的属性名,使用这样的结构可以进行数据存放. 使用data-*可以解决自定义属性混乱无管理的现状. ...

  3. HDU 2191 - 单调队列优化多重背包

    题目: 传送门呀传送门~ Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种 ...

  4. Flask最强攻略 - 跟DragonFire学Flask - 第十篇 before_request after_request

    Flask我们已经学习很多基础知识了,现在有一个问题 我们现在有一个 Flask 程序其中有3个路由和视图函数,如下: from flask import Flask app = Flask(__na ...

  5. pl/sql Devloper 关键字转为大写

    英文版: peferences-editor-keyword case -uppercase   中文版:

  6. oracle_使用子查询创建表

    create table emp_bk as (select * from emp where 1=2);这句就是复制源表的结构

  7. python中argparse

    python中argparse 在很多编程语言中,运行程序可以直接使用function(a,b,……)运行程序,但是在python中就无法实现,那么我们如何在命令行中传递参数呢?在python的中,有 ...

  8. freeRTOS中文实用教程4--资源管理概述

    1.前言 多任务系统中存在一种潜在的风险.当一个任务在使用某个资源的过程中,即还没有完全结束对资源的访问时,便被切出运行态,使得资源处于非一致,不完整的状态 2.并发抢占导致错误的场景 (1)访问外设 ...

  9. gdb revert, Go to previous line in gdb

    Yes! With the new version 7.0 gdb, you can do exactly that! The command would be "reverse-step& ...

  10. ES系列七、ES-倒排索引详解

    1.单词——文档矩阵 单词-文档矩阵是表达两者之间所具有的一种包含关系的概念模型,图3-1展示了其含义.图3-1的每列代表一个文档,每行代表一个单词,打对勾的位置代表包含关系. 图3-1 单词-文档矩 ...