天空小小岛技术论坛

 找回密码
 注册
搜索
查看: 10085|回复: 0
打印 上一主题 下一主题

[总结] php curl上传图片时Content-Type错误问题

[复制链接]
跳转到指定楼层
1#
八号 发表于 2014-10-16 17:25:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
当使用curl @ 来上传文件时
  1. <?php
  2. $fileUploadPostParams = array(
  3.     'file' => '@/123.jpg'
  4. );

  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_URL, 'http://tkxxd.com/post.php');
  7. curl_setopt($ch, CURLOPT_POST, true);
  8. curl_setopt($ch, CURLOPT_POSTFIELDS, $fileUploadPostParams);
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  10. echo curl_exec($ch);
复制代码


接收到文件类型变掉
  1. array (
  2.   'file' =>
  3.   array (
  4.     'name' => '123.jpg',
  5.     'type' => 'application/octet-stream',
  6.     'tmp_name' => '/tmp/php7Zr0bN',
  7.     'error' => 0,
  8.     'size' => 30163,
  9.   ),
  10. )
复制代码


type变为  application/octet-stream
不是image/jpeg 接收侧会判断为不合法,上传失败

解决方法:
通过在文件名后面加上";type=image/jpeg"解决了该问题,如'file' => '@/123.jpg;type=image/jpeg'

CURL 7.19.7支持此方法,7.16.0不支持此方法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|天空小小岛  |京ICP备2025130156号|

GMT+8, 2025-6-21 18:17 , Processed in 0.096511 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表