使用 cURL 将 PHP 文件上传到 imageshack

发布于 2024-12-25 01:42:56 字数 1041 浏览 0 评论 0原文

我正在尝试使用以下脚本将文件上传到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2025-01-01 01:42:56

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

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

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

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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文