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);
重新上传 问题解决
一、推荐使用迅雷或快车等多线程下载软件下载本站资源。
二、未登录会员无法下载,登录后可获得更多便利功能,若未注册,请先注册。
三、如果服务器暂不能下载请稍后重试!总是不能下载,请点我报错 ,谢谢合作!
四、本站大部分资源是网上搜集或私下交流学习之用,任何涉及商业盈利目的均不得使用,否则产生的一切后果将由您自己承担!本站将不对任何资源负法律责任.如果您发现本站有部分资源侵害了您的权益,请速与我们联系,我们将尽快处理.
五、如有其他问题,请加网站设计交流群(点击这里查看交流群 )进行交流。
六、如需转载本站资源,请注明转载来自并附带链接
七、本站部分资源为加密压缩文件,统一解压密码为:www.aizhanzhe.com
大家评论
站长推荐