当我使用重定向时,其他网站无法从我的网站上传图像
我有一个网站,其图像的网址如下:
http://mysite.com/image1.jpg
我的服务器使用 php 重定向到包含实际文件的子域,如下所示:
header('Location: http://images.mysite.com/image1.jpg');
这几乎适用于所有用途,例如在 img 标记中或保存文件时本地。对于这些目的来说,这两个 URL 本质上是等效的。但是,当我访问带有图像上传器的网站(例如 tumblr 或 imgur)时,我尝试上传第一个网址,但失败(显示“上传照片时出错”或“图像格式不受支持,或图像已损坏。”) 。
有办法让这项工作发挥作用吗?我尝试发送不同的状态标头,但它们没有帮助。
编辑:
按照建议,我可以通过 htaccess 重定向或打开文件并发送内容来完成此操作。这会工作得很好,除非我不希望当用户将文件用于其他目的时发生这种情况,例如将其嵌入 img 标签中。这是因为我的 images.mysite.com 域是一个用于托管我的图像内容的 CDN,如果我必须从我的 Web 服务器保留内容,它就会违背其目的。 htaccess 不可行,因为我想在请求第一个 url 时运行一些 php 脚本。
如果我能以某种方式检测到它何时通过文件上传器被使用,那么我可以单独处理其他情况。但我认为我无法检测到这一点,可以吗?
编辑2(解决方案):
好吧,我做了一个妥协:事实证明,上传请求通常在标头中没有“accept_charset”参数,而嵌入请求则有。使用此功能,我可以检测何时请求上传图像,然后下载该图像并发送内容。这不是一个坚如磐石的解决方案,但它运作良好。谢谢你们!
I have a website that has images with urls like this:
http://mysite.com/image1.jpg
My server uses php to do a redirect to a subdomain which contains the actual file, like this:
header('Location: http://images.mysite.com/image1.jpg');
This works fine for almost all purposes, like in an img tag or when saving the file locally. The two urls are essentially equivalent for these purposes. However, when I go to a site with an image uploader, like tumblr or imgur, and I try to upload the first url, it fails (saying "Error uploading photo" or "Image format not supported, or image is corrupt.").
Is there a way to make this work? I tried sending different status headers but they didn't help.
EDIT:
As suggested, I could do this with an htaccess redirect or by opening the file and sending the content. This would work fine except I don't want this to happen when users are using the files for other purposes, like embedding it in an img tag. This is because my images.mysite.com domain is a cdn for hosting my image content, and it would defeat it's purpose if I had to reserve the content from my web server. And htaccess is not viable because I wanted to run some php scripts when the first url is requested.
If I could somehow detect when it's being used through a file uploader, then I could handle the other cases separately. But I don't think I can detect that, can I?
EDIT 2 (SOLUTION):
Okay, I did a hacky compromise: it turns out that upload requests generally don't have an "accept_charset" parameter in the header, whereas embed requests do. Using this I can detect when an image is being requested to be uploaded, and then I download that image and send out the content. Not a rock-solid solution, but it works well enough. Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的图像上传网站似乎没有遵循您所做的重定向。因此,为了使其正常工作,您应该打开远程文件并发送其内容,而不是进行重定向。
It appears that your image uploader site doesn't follow the redirection you made. So, to make it works, instead of having a redirection, you should open your distant file and send its content.
您无法完成此操作,因为图像上传服务需要实际的图像文件,它们不遵循任何重定向。
You cannot make this work, because the image upload services expect an actual image file, they do not follow any redirects.
您不应该将标准结果代码(200 - OK)与标头重定向一起使用。
看看这个关于如何发送结果代码的教程 。
You shouldn't use standard result code (200 - OK) with header-redirects.
Have a look at this tutorial about how to send result codes.
正如其他发帖者所说,图像上传服务将您的 url 返回的数据读取
为图像文件 - 这就是重定向混淆这些服务的原因。
为什么不使用 PHP 脚本从外部源读取图像,然后打印出二进制数据,而不是重定向呢?您可以使用 .htaccess 文件来完成此操作,该文件查找请求中带有“.jpg”的任何内容。
As other posters have said, image upload services read the data returned by your url
as an image file - which is why the redirect confuses these services.
Instead of a redirect, why not use a PHP script that reads the image from the external source, and then prints out the binary data? You can accomplish this with an .htaccess file that looks for anything with ".jpg" in the request.