php填坑记之curl无法上传文件

今天上传文件   测试环境上传无误  正式环境确接受不到文件信息


代码如下

$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);
$aStatus = curl_getinfo($ch);


通过输出$aStatus 发现问题如下


测试环境下上传结果如下

{ 
["url"]=> string(76) "http://xxxxx" 
["content_type"]=> string(31) "application/json; charset=utf-8" 
["http_code"]=> int(200) 
["header_size"]=> int(465) 
["request_size"]=> int(381)
["filetime"]=> int(-1) 
["ssl_verify_result"]=> int(0) 
["redirect_count"]=> int(0) 
["total_time"]=> float(2.824019) 
["namelookup_time"]=> float(2.1E-5) 
["connect_time"]=> float(0.03058) 
["pretransfer_time"]=> float(0.030958) 
["size_upload"]=> float(836420) 
["size_download"]=> float(61) 
["speed_download"]=> float(21) 
["speed_upload"]=> float(296180) 
["download_content_length"]=> float(-1) 
["upload_content_length"]=> float(836420) 
["starttransfer_time"]=> float(0.059133) 
["redirect_time"]=> float(0) 
["redirect_url"]=> string(0) "" 
["primary_ip"]=> string(12) "xxx" 
["certinfo"]=> array(0) { } 
["primary_port"]=> int(80) 
["local_ip"]=> string(12) "xxxx" 
["local_port"]=> int(53530) 
} 


正式环境如下

{ 
["url"]=> string(76) "http://xxxxx" 
["content_type"]=> string(31) "application/json; charset=utf-8" 
["http_code"]=> int(400) 
["header_size"]=> int(465) 
["request_size"]=> int(381)
["filetime"]=> int(-1) 
["ssl_verify_result"]=> int(0) 
["redirect_count"]=> int(0) 
["total_time"]=> float(2.824019) 
["namelookup_time"]=> float(2.1E-5) 
["connect_time"]=> float(0.03058) 
["pretransfer_time"]=> float(0.030958) 
["size_upload"]=> float(836420) 
["size_download"]=> float(61) 
["speed_download"]=> float(21) 
["speed_upload"]=> float(578) 
["download_content_length"]=> float(-1) 
["upload_content_length"]=> float(678) 
["starttransfer_time"]=> float(0.059133) 
["redirect_time"]=> float(0) 
["redirect_url"]=> string(0) "" 
["primary_ip"]=> string(12) "xxx" 
["certinfo"]=> array(0) { } 
["primary_port"]=> int(80) 
["local_ip"]=> string(12) "xxxx" 
["local_port"]=> int(53530) 
} 


注意看标出的部分 线上环境根本没有上传文件   排查问题  发现问题所在如下

老版本php的curl支持通过在数组数据中,使用“@+文件全路径”的语法附加文件

但php从5.5开始引入了新的CURLFile类用来指向文件

php 5.5另外引入了CURL_SAFE_UPLOAD选项 可以强制php的curl模块拒绝旧的@语法,仅接受CURLFile式的文件。5.5的默认值为false,5.6的默认值为true


测试环境vesion5.5  线上环境为5.6  所以出问题了  修改如下

$ch = curl_init();
$data = array('name' => 'Foo', 'file' => new CURLFille('/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);
$aStatus = curl_getinfo($ch);

重新上传 问题解决

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值