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. W3C规范

    连接:https://www.w3cschool.cn/xuexiw3c/xuexiw3c-standards.html W3C 代码标准规范 由 路飞 创建, 最后一次修改 2017-01-03 W ...

  2. 第14月第30天 svn 撤销ignore revert

    1. 直接到被ignore的位置,执行: svn add <你被ignore的文件名> --no-ignore –no-ignore是取消忽略 如果是add目录,你可以: svn add ...

  3. 转载 int和string 类型的互换

    https://blog.csdn.net/u012421436/article/details/51386690 不论是在什么语言下编程(除C,因为C是没有string类型的),int与string ...

  4. DMA及cache一致性的学习心得 --dma_alloc_writecombine【转】

    转自:https://www.cnblogs.com/hoys/archive/2012/02/17/2355914.html 来源:http://xmxohy.blog.163.com/blog/s ...

  5. c或c++的网络库

    Asio C++ Library: Asio is a cross-platform C++ library for network and low-level I/O programming tha ...

  6. 如何交叉编译 linux kernel 内核

    Compilation We first need to move the config file by running cp arch/arm/configs/bcmrpi_cutdown_defc ...

  7. TCP template 代码

    服务端 from socket import * server= socket(AF_INET,SOCK_STREAM) server.bind(('127.0.0.1',8080)) server. ...

  8. robots.txt、humans.txt、.editorconfig、.gitignore、LICENSE.txt、README.md、CHANGLOG.md

    robots.txt搜索引擎查看的时候会查看这个文件,告诉搜索引擎哪些文件可以查看,哪些文件不能查看 当搜索引擎搜索网站的时候,会看有这个文件没,如果有,会通过里面的文件来确定哪些文件能看,哪些文件不 ...

  9. 错误/异常:java.io.FileNotFoundException: .\src\db.properties (系统找不到指定的路径。);的解决方法

    1.异常视图 2.解决方法 与之相关的部分代码: static{ try { //读取db.properties Properties props = new Properties(); FileIn ...

  10. SqlServer 2017 下载地址及密钥

    下载地址: ed2k://|file|cn_sql_server_2017_developer_x64_dvd_11296175.iso|1769777152|E21AE7C3576C0BDF1BC0 ...