php curl上传文件$_FILES为空问题
php使用curl上传文件,代码如下:
发送的代码(完全是官方的示例)
<?php
/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/home/vagrant/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
接收代码(也是官方的)
<?php
print_r($_POST);
print_r($_FILES);
运行结果
php -f demo.php
Array
(
[name] => Foo
[file] => @/home/vagrant/test.png
)
Array
(
)
解决方法1:
<?php
/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/home/vagrant/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
解决方法2:
5.6版本下
<?php
/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => new \CURLFile(realpath('/home/vagrant/test.png')));
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
相关文章:
php curl文件上传兼容php5.0~5.6各版本
http://www.cnblogs.com/zqifa/p/php-curl-2.html
php curl上传文件$_FILES为空问题的更多相关文章
- PHP如何通过CURL上传文件
PHP使用CURL上传文件只需发送一个POST请求就可以了,在请求中设置某个字段为需要上传的文件全路径,并且以“@”开头,然后使用CURL把该变量以POST方式发送到服务器,在服务端即可以从超级全局变 ...
- curl 上传文件
1)在 php 5.5.0 之前,如果使用 @+文件路径的文件上传文件,具体看这里:http://www.cnblogs.com/tujia/p/5938463.html 2)php 5.5.0 之后 ...
- PHP 5.6 如何使用 CURL 上传文件
以前我们通过 PHP 的 cURL 上传文件是,是使用“@+文件全路径”的来实现的: curl_setopt(ch, CURLOPT_POSTFIELDS, array( 'file' => ' ...
- PHP 5.5以上 使用 CURL 上传文件
PHP 5.5以上 使用 CURL 上传文件的代码: curl_setopt(ch, CURLOPT_POSTFIELDS, [ 'file' => new CURLFile(realpath( ...
- php+curl上传文件
因为公司项目用java做的,需要我这边用php上传文件.只给了个接口,参数都不明确,然后这边不提交表单,在生成pdf之后就立马上传.用了php+curl,总是没上传成功,这里看到了篇文章http:// ...
- 关于PHP7的CURL上传文件
CULR 部分 $url = 'http://localhost/test/curlUploadFile/upload.php'; //处理上传的php文件,根据情况修改 $path='ftp.txt ...
- Curl上传文件
curl -v -XPOST -H /thumbnail
- PHP使用CURL上传|下载文件
CURL下载文件 /** * @param string $img_url 下载文件地址 * @param string $save_path 下载文件保存目录 * @param string $fi ...
- 使用PHP处理POST上传时$_FILES数组为何为空
在做一个简单的表单上传测试时,服务端的php脚本中,$_FILES数组为空;这样就不能获取从浏览器上传的信息.什么原因呢? 通过Google,找到下面这个web: php上传文件$_FILES数组为空 ...
随机推荐
- http1.0 1.1 2.0区别
http1.0 1.1 2.0区别 转载:https://blog.csdn.net/linsongbin1/article/details/54980801/ 1.HTTP1.0 1.1区别 (1) ...
- 标准库中 vector list等排序
1.list自带有排序函数sort():可以定义自己的排序规则,如: struct stTest { int count; wstring str; }; bool SortByNum(const s ...
- linux processes identifiers
Linux, like all Unix uses user and group identifiers to check for access rights to files and images ...
- 快速切题 sgu113 Nearly prime numbers 难度:0
113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...
- hdu4333
题解: EX_KMP 先把串复制一遍放到后面 这样旋转就是每一个前缀了 然后做一个EX_KMP 然后看一下后一个字符谁大谁小 代码: #include<cstdio> #include&l ...
- bzoj1625
题解: 简单dp 要一维 代码: #include<bits/stdc++.h> using namespace std; ; int n,m,a[N],b[N],f[N]; int ma ...
- bzoj1215
题解: 暴力枚举每一种方案,然后hash判重 代码: #include<bits/stdc++.h> #define eps 1e-7 using namespace std; ],r[] ...
- Spring学习笔记之Testing
测试嘛,一般也就两种,一种就是单元测试,另外一个就是集成测试.都是废话 一.单元测试 以前也就是搞个模拟,main函数一写搞定. 现在呢,有了个spring,也有了个推荐规范?这个是个什么东西?什么叫 ...
- Chrome 应用推荐 - Shortcuts for Google™ 3.4.1
Choose from 200+ Google™ services to show up as buttons in a space-saving popup. Official Firefox ex ...
- SharePoint 网站管理-PowerShell
1. 显示场中所有可用的网站集 Get-SPSite 2. 显示某一Web应用程序下可用的网站集 Get-SPSite –WebApplication "SharePoint – 80&qu ...