curl 文件上传
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 文件上传的更多相关文章
- curl文件上传有两种方式,一种是post_fileds,一种是infile
curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄! );curl_setopt($ch, CURLOPT_PO ...
- php curl文件上传兼容php5.0~5.6各版本
PHP 5.0~5.6 各版本兼容的cURL文件上传 最近做的一个需求,使用PHP cURL上传文件.踩坑若干,整理如下. 不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POS ...
- [转]考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传
FROM : https://segmentfault.com/a/1190000000725185 最近做的一个需求,要通过PHP调用cURL,以multipart/form-data格式上传文件. ...
- PHP 5.0~5.6 各版本兼容性的 cURL 文件上传
不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POSTFIELDS传递关联数组(而不是字符串)来生成multipart/form-data的POST请求. 传统上,PHP的cU ...
- 考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传
最近做的一个需求,要通过PHP调用cURL,以multipart/form-data格式上传文件.踩坑若干,够一篇文章了. 重要警告 没事不要读PHP的官方中文文档!版本跟不上坑死你! 不同版本PHP ...
- php curl文件上传
<?php /** * 这是一个自动化部署的类, 非常简单,思想就是压缩,上传,然后解压覆盖,所以请小心使用. * @author liuchao <249757247@qq.com> ...
- 【转载】兼容php5,php7的cURL文件上传示例
转载来自: http://www.huanlinna.com/2016/06/25/coding/php5-php7-upload-demo-via-curl.html https://segment ...
- php 使用curl 将文件上传
<?php /** * curl文件上传 * @var struing $r_file 上传文件的路劲和文件名 */ function upload_file($r_f ...
- 淘宝SDK扒出来的CURL调用含文件上传代码
<?php function curl($url,$postFields=null){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); c ...
随机推荐
- windows钩子函数
一 什么时候用到钩子?(when)Windows操作系统是建立在事件驱动的消息处理机制之上,系统各部分之间的沟通也都是通过消息的相互传递而实现的.通常情况下,应用程序只能处理当前进程的消息,如果需要对 ...
- proxysql 系列~审核功能
一 简介:今天我们来探讨下具体的审核功能 二 平台审计功能 一 proxysql 设置 set mysql-eventslog_filename = '/data/ProxySQL/log/sql. ...
- python - 自定制property/property的延时计算
自定制prooerty: #模拟@property 实现将类的函数属性变成类属性: #定义描述符 class msf(): def __init__(self,obj): self.obj = obj ...
- [转]xargs命令详解,xargs与管道的区别
为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...
- c# 登录 防止sql注入 mysql数据库
利用参数化 防止SQL注入 public string serachName(string name) { string result = ""; try { conn.Open( ...
- pl/sql Devloper 快捷键__新建sql窗口
首先,打开PLSQL,菜单栏--->首选项----->键配置 其次,点击你要增加快捷键的选项,直接键盘上输入快捷键: 比如你要修改为CTRL+N,直接在键盘上按出CTRL+N即可. ESC ...
- TeamCity 和 Nexus 的使用
参考:http://www.jianshu.com/p/255a484555d9 TeamCity 安装部署(Linux 环境) 在我讲之前,如果你英文还可以,就到官网这里看下: Installati ...
- 记一次Win Server 2012部署问题及解决方法
1.前言 本章内容为在win server 2012服务器部署时遇到的问题及解决方法.大致工作为:两台服务器,一台web.一台数据库:web服务器部署.net web程序,数据库服务器安装oracle ...
- 【工具测试】Acunetix 11-登录后扫描的功能
1.概要 在测试的过程中,会给一些只有登录口的测试站点,只有登录后才能访问更多的页面. Acunetix 11的登录后扫描功能摸索了老半天,原来这么神奇.学习了! 2.操作 登录之后 - [Add T ...
- 【转】Python数据类型之“数字(numerics)”
[转]Python数据类型之“数字(numerics)” 上一节内容说的是“Python基本语法”,本节主要讲下Python中的数据类型. 存储在内存中的数据通常有两个属性: 在内存中的存放位置:这个 ...