Can I use images without extension in ? [

2019-02-03 01:57发布

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

4条回答
不美不萌又怎样
2楼-- · 2019-02-03 02:08

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.

查看更多
萌系小妹纸
3楼-- · 2019-02-03 02:09

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.

查看更多
家丑人穷心不美
4楼-- · 2019-02-03 02:14

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.

查看更多
萌系小妹纸
5楼-- · 2019-02-03 02:15

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

查看更多
登录 后发表回答