drv54591 2012-01-06 15:34
浏览 43

使用cURL将带有PHP的文件上传到ima​​geshack

I am trying to upload a file to imageshack using following script:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://post.imageshack.us/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    'fileupload'=> '@../../resources/images/cancel.png',
    'optsize' => 'resample',
    'rembar' => '1'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

However, I get following error from imageshack:

Wrong file type detected for file cancel.png:application/octet-stream.

Why is that, and how can it be fixed? Thanks in advance!

EDIT: It does work when I use JPGs or GIFs (even animated ones, although Imageshack then de-animates them, I think by only using the first frame), but not with PNGs.

EDIT 2: I found out how to fix it. As stated earlier, it does only accept JPGs and GIFs. So I take the location of the image I need to upload, copy it to a temporary location with the ending .jpg, upload the file, and then, upon completion, I remove the file from the temporary location.

  • 写回答

1条回答 默认 最新

  • dtmm0148603 2012-01-06 15:48
    关注

    Set the Content-type to the type of the file.

    According to the manual, it must be an array or object. So here

    $arr = array('Content-type: text/plain') ;
    curl_setopt ( $ch , CURL_HTTPHEADER , $arr );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记