使用 cURL 将 PHP 文件上传到 imageshack
我正在尝试使用以下脚本将文件上传到 imageshack:
$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 收到以下错误:
检测到文件 cancel.png:application/octet-stream 的文件类型错误
。
为什么会这样?如何解决?提前致谢!
编辑: 当我使用 JPG 或 GIF 时(甚至是动画的,尽管 Imageshack 然后将它们取消动画,我认为仅使用第一帧),它确实有效,但不适用于 PNG。
编辑2: 我找到了如何修复它。如前所述,它只接受 JPG 和 GIF。因此,我获取需要上传的图像的位置,将其复制到以 .jpg 结尾的临时位置
,上传文件,然后在完成后,我从临时位置删除该文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 Content-type 设置为文件的类型。
根据手册,它必须是一个数组或对象。所以在这里
Set the Content-type to the type of the file.
According to the manual, it must be an array or object. So here