您的位置:首页 > 编程语言 > PHP开发

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

2017-06-30 16:44 330 查看
今天上传文件   测试环境上传无误  正式环境确接受不到文件信息

代码如下

[sql] view
plain copy

$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 发现问题如下

测试环境下上传结果如下

[sql] view
plain copy

{   

["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)   

<span style="background-color: rgb(255, 0, 0);">["upload_content_length"]=> float(836420) </span>  

["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)   

}   

正式环境如下

[sql] view
plain copy

{   

["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)   

<span style="background-color: rgb(255, 0, 0);">["upload_content_length"]=> float(678) </span>  

["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  所以出问题了  修改如下

[sql] view
plain copy

$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);  

重新上传 问题解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php5 curl php7 php 函数