网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
07月31日漏签0天
php吧 关注:280,616贴子:1,321,048
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 11回复贴,共1页
<<返回php吧
>0< 加载中...

一个curl POST文件的问题,求大神解答

  • 只看楼主
  • 收藏

  • 回复
  • soonerman
  • Warning
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
test2.php模拟post发送文件到test.php,接收到后只有POST数组,FILES是空的。
下面是test2.php的代码
$ch = curl_init();
$post_data = array(
*filename* => *qie.jpg*,
*file* => @*E:/WEB/PHP/tools/grab/qie.jpg*
);
curl_setopt($ch, CURLOPT_HEADER, false)
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_URL, *http://localhost/PHP/tools/grab/test.php*);
$info = curl_exec($ch);
curl_close($ch)
print_r($info);
下面是test.php页面的代码:
print_r($_POST);
echo *===file upload info:*;
print_r($_FILES);
下面是输出结果:
Array
(
[filename] => qie.jpg
[file] => @E:/WEB/PHP/tools/grab/qie.jpg
)
===file upload info:Array
(
)
1
为什么只有POST数据,没有FILES数据呢?
昨天晚上弄了一晚上也没弄明白,网上的代码也都是这样,都说能上传成功。求大神围观,谢谢解答!


  • 雷军
  • 继承
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
少了两个;


2025-07-31 01:26:04
广告
不感兴趣
开通SVIP免广告
  • 月下的磊
  • Warning
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
要设置请求类型为 multipart/form-data才行


  • soonerman
  • Warning
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
问题解决了,查了半天,终于在某个地方看到这么一段话:
The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with@ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ‘;type=mimetype‘. This parameter can either be passed as a urlencoded string like ‘para1=val1&para2=val2&…‘ or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile.
原来我的php版本问题,5.5以上用@加全路径的方法已经被弃用了,需要用CURLFile creat一下文件,难怪连知道里面也没一个人回答。


  • 诸葛益智🐾
  • 记事本
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
CURLOPT_POSTFIELDS这个参数如果是POST字符串时,可以用形如“name=value&...”的字符串,如果post文件,就必须要用数组,并且文件名格式为"@绝对路径",这是 CURL 会帮你做 multipart/form-data 编码。


  • 诸葛益智🐾
  • 记事本
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
<?php
$target="http://youraddress.tld/example/upload.php";
# http://php.net/manual/en/curlfile.construct.php
// Create a CURLFile object / procedural method
$cfile = curl_file_create('resource/test.png','image/png','testpic'); // try adding
// Create a CURLFile object / oop method
#$cfile = new CURLFile('resource/test.png','image/png','testpic'); // uncomment and use if the upper procedural method is not working.
// Assign POST data
$imgdata = array('myimage' => $cfile);
$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); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true); // enable posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); // post images
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
$r = curl_exec($curl);
curl_close($curl);
?>


  • BlackLAM酒
  • Warning
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
棒棒棒棒棒棒


  • l317180396
  • 路过酱油
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
万分感谢,困扰两天的问题,各种搜,终于解决了,谢谢,太感谢了


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 11回复贴,共1页
<<返回php吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示