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

来源:互联网 发布:易源网络验证 编辑:程序博客网 时间:2024/06/14 14:24

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


代码如下

[sql] view plain copy
  1. $ch = curl_init();  
  2. $data = array('name' => 'Foo''file' => '@/home/vagrant/test.png');  
  3. curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');  
  4. curl_setopt($ch, CURLOPT_POST, 1);  
  5. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);  
  6. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  7. curl_exec($ch);  
  8. $aStatus = curl_getinfo($ch);  


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


测试环境下上传结果如下

[sql] view plain copy
  1. {   
  2. ["url"]=> string(76) "http://xxxxx"   
  3. ["content_type"]=> string(31) "application/json; charset=utf-8"   
  4. ["http_code"]=> int(200)   
  5. ["header_size"]=> int(465)   
  6. ["request_size"]=> int(381)  
  7. ["filetime"]=> int(-1)   
  8. ["ssl_verify_result"]=> int(0)   
  9. ["redirect_count"]=> int(0)   
  10. ["total_time"]=> float(2.824019)   
  11. ["namelookup_time"]=> float(2.1E-5)   
  12. ["connect_time"]=> float(0.03058)   
  13. ["pretransfer_time"]=> float(0.030958)   
  14. ["size_upload"]=> float(836420)   
  15. ["size_download"]=> float(61)   
  16. ["speed_download"]=> float(21)   
  17. ["speed_upload"]=> float(296180)   
  18. ["download_content_length"]=> float(-1)   
  19. <span style="background-color: rgb(255, 0, 0);">["upload_content_length"]=> float(836420) </span>  
  20. ["starttransfer_time"]=> float(0.059133)   
  21. ["redirect_time"]=> float(0)   
  22. ["redirect_url"]=> string(0) ""   
  23. ["primary_ip"]=> string(12) "xxx"   
  24. ["certinfo"]=> array(0) { }   
  25. ["primary_port"]=> int(80)   
  26. ["local_ip"]=> string(12) "xxxx"   
  27. ["local_port"]=> int(53530)   
  28. }   


正式环境如下

[sql] view plain copy
  1. {   
  2. ["url"]=> string(76) "http://xxxxx"   
  3. ["content_type"]=> string(31) "application/json; charset=utf-8"   
  4. ["http_code"]=> int(400)   
  5. ["header_size"]=> int(465)   
  6. ["request_size"]=> int(381)  
  7. ["filetime"]=> int(-1)   
  8. ["ssl_verify_result"]=> int(0)   
  9. ["redirect_count"]=> int(0)   
  10. ["total_time"]=> float(2.824019)   
  11. ["namelookup_time"]=> float(2.1E-5)   
  12. ["connect_time"]=> float(0.03058)   
  13. ["pretransfer_time"]=> float(0.030958)   
  14. ["size_upload"]=> float(836420)   
  15. ["size_download"]=> float(61)   
  16. ["speed_download"]=> float(21)   
  17. ["speed_upload"]=> float(578)   
  18. ["download_content_length"]=> float(-1)   
  19. <span style="background-color: rgb(255, 0, 0);">["upload_content_length"]=> float(678) </span>  
  20. ["starttransfer_time"]=> float(0.059133)   
  21. ["redirect_time"]=> float(0)   
  22. ["redirect_url"]=> string(0) ""   
  23. ["primary_ip"]=> string(12) "xxx"   
  24. ["certinfo"]=> array(0) { }   
  25. ["primary_port"]=> int(80)   
  26. ["local_ip"]=> string(12) "xxxx"   
  27. ["local_port"]=> int(53530)   
  28. }   


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

老版本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
  1. $ch = curl_init();  
  2. $data = array('name' => 'Foo''file' => new CURLFille('/home/vagrant/test.png'));  
  3. curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');  
  4. curl_setopt($ch, CURLOPT_POST, 1);  
  5. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);  
  6. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  7. curl_exec($ch);  
  8. $aStatus = curl_getinfo($ch);  

重新上传 问题解决

原创粉丝点击