天空小小岛技术网站

标题: php curl上传图片时Content-Type错误问题 [打印本页]

作者: 八号    时间: 2014-10-16 17:25
标题: php curl上传图片时Content-Type错误问题
当使用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不支持此方法




欢迎光临 天空小小岛技术网站 (http://tkxxd.net/) Powered by Discuz! X3.1