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

时间:2012-01-06 15:34:22

标签: php file curl upload imageshack

我正在尝试使用以下脚本将文件上传到ima​​geshack:

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

但是,我从imageshack得到以下错误:

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

为什么会这样,以及如何解决?提前谢谢!

修改 当我使用JPG或GIF(甚至是动画的,虽然Imageshack然后去动画它们,我认为只使用第一帧)时它确实有效,但不是使用PNG。

编辑2: 我发现了如何解决它。如前所述,它只接受JPG和GIF。因此,我获取需要上传的图像的位置,将其复制到临时位置,结尾为.jpg ,上传文件,然后,完成后,我从中删除文件临时位置。

1 个答案:

答案 0 :(得分:2)

将Content-type设置为文件类型。

根据手册,它必须是数组或对象。所以这里

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