我可以在 中使用不带扩展名的图像吗?

发布于 2024-10-19 03:15:42 字数 640 浏览 1 评论 0原文

可能的重复:
提供服务是否安全网络上的图像没有扩展名?

我想在页面中使用类似 的内容。 FireFox 不会显示它,这让我觉得我必须有该文件的文件扩展名。我在这里吗(也适用于其他浏览器),还是 FF 喜欢 mime 类型?

编辑

我已经尝试了各种方法,但仍然不起作用。我现在正确地为文件添加了扩展名(例如,Flash 的.swf)。我已经更改了目录等。当我调用 file_exists() 时,文件就在那里,一切都很高兴,但是我绝对无法让它在页面上呈现。它可以是 img 标记中的 .PNG,也可以是 Flash 对象。两者都不起作用。我做错了什么:-( 另外,如果我将未上传的文件重命名为脚本正在查找的文件,效果很好,但上传的文件则不然...

Possible Duplicate:
Is it safe to serve an image on the web without an extension?

I'd like to use something like <img src="advertisements/12" style="border: 1px solid"> in a page. FireFox won't display it, which makes me think I have to have a file extension for the file. Am I right here (for the other browsers too), or does FF like mime types?

EDIT

I've tried all sorts of stuff and it still won't work. I now put an extension on the file correctly (.swf for Flash, for example). I've changed the directory, etc etc. When I call file_exists(), The file is there, all happy and such, however I absolutely cannot get it to render on the page. It could either be a .PNG in an img tag, or a Flash object. Neither works. What am I doing wrong :-(
Also, if I rename a non-uploaded file to the file the script is looking for, that works fine, but the uploaded ones don't...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

戴着白色围巾的女孩 2024-10-26 03:15:42

是的,你应该能够。

浏览器最初不查看文件名,而是查看 HTTP 服务器响应中 Content-type 标头中列出的 MIME 类型。如果内容类型是 image/jpeg、png 或 gif 等,那么一切都很好,它会愉快地渲染图像。

如果没有有效的 MIME 类型,某些浏览器将尝试猜测类型是什么。他们将查看所请求的对象的扩展名,或者他们将查看前几个字节。如果失败,那么你会得到一个 redex。

这在 Firefox / Firebird 的早期曾引起一些麻烦,因为它对 MIME 类型很严格,并且经常拒绝渲染没有有效 MIME 类型的内容。 IE 进行了猜测,许多草率的 Web 服务器所提供的页面在 IE 中可以正常呈现,但在其他浏览器中则不然。不过现在,情况好多了。

因此,只要您的 Web 服务器在请求 img 对象时提供正确的 MIME 类型,您就可以顺利进行。如果您遇到问题,请检查您的网络服务器在请求“advertisements/12”时正在做什么。

Yes, you should be able to.

Browsers initially don't look at the filename, they look at the MIME type listed in the Content-type header in the response from the HTTP server. If the content type is image/jpeg, or png or gif or whatever, things are good and it will happily render the image.

If there is no valid MIME type, some browsers will try to guess what the type is. They'll look at the extension of object being request, or they'll look at the first few bytes. If that fails, then you get a redex.

That used to cause some woes back in the early days of Firefox / Firebird, because it was strict about mime types and often refused to render something without a valid MIME type. IE made a guess, and so many sloppy web servers would serve up pages that would render fine in IE, but not in others. These days though, things are much better.

So, as long as your web server is providing the right MIME type when the img object is requested, you'll be good to go. If you are having problems, check what your web server is doing when "advertisements/12" is requested.

少女七分熟 2024-10-26 03:15:42

服务器返回什么?文件扩展名实际上没有任何用途。浏览器正在检查 Content-type 标头,该标头应该类似于 image/jpeg 或您正在提供的任何类型的图像。

这在动态提供图像(通常来自数据库)的网站中经常使用。我见过很多像 image.aspx?id=37 这样的图像 URL,虽然它在技术上有一个“扩展名”,但并不意味着它是一个图像。这一切都在 HTTP 标头值中。

What is the server returning? The file extension isn't used for anything, really. The browser is checking for the Content-type header, which should be something like image/jpeg or whatever type of image you're serving.

This is used pretty often in sites which dynamically serve images, often from a database. I've seen plenty of image URLs like image.aspx?id=37 which, while it technically has an "extension" doesn't really mean that it's an image. It's all in the HTTP header values.

那些过往 2024-10-26 03:15:42

提供 MIME 类型可能会有所帮助,尽管理论上服务器应该提供正确的类型。如果是提供图像的专用 PHP(或类似)脚本,则必须确保将 HTTP Content-Type 标头设置为适当的 MIME 类型。

如果您想避免使用 标记,可以将

与 CSS 背景结合使用,但如果浏览器不这样做,这将无济于事无法将 advertisements/12 识别为任何已知的图像类型。

Providing a MIME type might help, although the server should theoretically be providing the correct one. If it's a specialized PHP (or similar) script that's serving up the image, you have to make sure you set the HTTP Content-Type header to the appropriate MIME type.

If you want to avoid using the <img> tag, you can use <div> in conjuntion with CSS backgrounds, but that's not going to help if the browser doesn't recognize advertisements/12 as any known image type.

梦明 2024-10-26 03:15:42

2022 编辑:

哇!好吧,当时它工作得很好......但是,对于现代检测来说,有 https://www.php.net/manual/en/function.exif-imagetype.php ...从 v7.1 开始具有 webp 检测。


在重命名从网络表单上传的图像后,我发现自己遇到了类似的图像查看问题。由于我可以混合使用 gif、jpg、jpeg 或 png 文件,因此我查看了文件属性。

我最终做的是检查 MIME 类型(在处理上传的文件时通常位于 $_FILES 数组内)并向文件名附加关联的扩展名。

if($_FILES[$fieldname]['type'][$key]=='image/gif') {
     $ext='.gif';
}

if($_FILES[$fieldname]['type'][$key]=='image/jpeg') {
     $ext='.jpg';
}

if($_FILES[$fieldname]['type'][$key]=='image/pjpeg') {
     $ext='.jpg';
}

if($_FILES[$fieldname]['type'][$key]=='image/png') {
     $ext='.png';
}

常见图像 MIME 类型列表。

--这确实解决了我的问题,因为我的浏览器是尝试将我的图像下载为不带文件扩展名的二进制文件。

服务器 - Debian、Apache 2.2、PHP 5.3

Web 客户端 - Kubuntu 11.10、Firefox

2022 Edit:

Wow! Well, it worked good enough back then... But, for modern day detection there is https://www.php.net/manual/en/function.exif-imagetype.php ...with webp detection as of v7.1.


I found myself with similar image viewing problems after renaming images uploaded from web forms. Since I could have a mix of gif, jpg, jpeg, or png files I went looking at the file properties.

What I ended up doing was checking the MIME type (which is usually within the $_FILES array while processing the uploaded files) and appending an associated extension to the file name.

if($_FILES[$fieldname]['type'][$key]=='image/gif') {
     $ext='.gif';
}

if($_FILES[$fieldname]['type'][$key]=='image/jpeg') {
     $ext='.jpg';
}

if($_FILES[$fieldname]['type'][$key]=='image/pjpeg') {
     $ext='.jpg';
}

if($_FILES[$fieldname]['type'][$key]=='image/png') {
     $ext='.png';
}

List of common image mime types.

--Which did fix my problem as my browser was trying to download my images as binary files without the filename extension.

Server - Debian, Apache 2.2, PHP 5.3

Web Client - Kubuntu 11.10 ,Firefox

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