Html 我可以在 <img> 中使用没有扩展名的图像吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5110384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:55:22  来源:igfitidea点击:

Can I use images without extension in <img>?

htmlimagefile-extension

提问by Bojangles

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?

我想<img src="advertisements/12" style="border: 1px solid">在页面中使用类似的东西。FireFox 不会显示它,这让我觉得我必须为该文件添加文件扩展名。我就在这里(对于其他浏览器也是如此),还是 FF 喜欢 mime 类型?

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 imgtag, 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...

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

采纳答案by whatsisname

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.

浏览器最初不会查看文件名,而是查看 HTTP 服务器响应中 Content-type 标头中列出的 MIME 类型。如果内容类型是图像/jpeg,或 png 或 gif 或其他,一切都很好,它会很高兴地呈现图像。

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.

如果没有有效的 MIME 类型,一些浏览器会尝试猜测类型是什么。他们会查看请求对象的扩展名,或者他们会查看前几个字节。如果失败,那么你会得到一个 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.

在 Firefox / Firebird 的早期,这曾经导致一些麻烦,因为它对 mime 类型很严格,并且经常拒绝在没有有效 MIME 类型的情况下呈现某些内容。IE 进行了猜测,因此许多草率的 Web 服务器会提供在 IE 中呈现良好的页面,但在其他页面中则不然。不过,这些天,情况好多了。

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.

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

回答by David

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

服务器返回什么?文件扩展名不用于任何事情,真的。浏览器正在检查Content-type标题,它应该是类似image/jpeg或您提供的任何类型的图像。

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=37which, while it technically has an "extension" doesn't really mean that it's an image. It's all in the HTTP header values.

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

回答by digitlworld

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.

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

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/12as any known image type.

如果您想避免使用该<img>标签,您可以<div>结合 CSS 背景使用,但如果浏览器无法识别advertisements/12任何已知的图像类型,这将无济于事。

回答by BradChesney79

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.

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

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.

我最终做的是检查 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';
}

List of common image mime types.

常见图像 MIME 类型列表。

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

服务器 - Debian、Apache 2.2、PHP 5.3

Web Client - Kubuntu 11.10 ,Firefox

Web 客户端 - Kubuntu 11.10,Firefox