41

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

Community
  • 1
  • 1
Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • 2
    Does "advertisements/12" send the right `Content-type` HTTP header for the type of image it is? I don't think the browser cares about extensions at all. – Dan Grossman Feb 24 '11 at 21:00
  • yes, you can. but have in mind that this will annoy users if they ever decide to save the images in their filesystems. – Hugo Mota Sep 23 '12 at 18:48

4 Answers4

38

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.

whatsisname
  • 5,872
  • 2
  • 20
  • 27
10

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.

David
  • 208,112
  • 36
  • 198
  • 279
  • Right, my previous answer was saying that the server itself will not necessarily set that header correctly if the file you're serving doesn't have a recognized extension. It's not going to read the file and try to determine what it is. – digitlworld Feb 24 '11 at 21:07
  • @Digitlworld: True, for regular files the web server will handle that sort of thing. For custom content like this, either the server will need to be configured to know to provide the header or the application running on the server will need to do it manually. (I usually prefer the latter in my setups.) Of course, the question doesn't specify any information regarding the server of any application framework running on it... – David Feb 24 '11 at 21:35
  • The server is running Apache2 with PHP5. The files are actually just standard images or Flash (.SWF) files. They just don't (didn't) have an extension. – Bojangles Feb 25 '11 at 12:07
  • @JamWaffles: Cool. It sounds like it would be easier, at least in my opinion, to handle the headers from within the PHP application. Unless the URLs are easily predictable and won't change and you'd prefer to do it in the Apache config, that's up to you. – David Feb 25 '11 at 12:11
2

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.

digitlworld
  • 1,046
  • 7
  • 13
  • It's just an image stored on disk. The mime type is fine on my file browser (Nautilus) – Bojangles Feb 24 '11 at 21:02
  • 1
    Check the mime type that shows up when the image is actually served. Apache and similar often decide that based purely on extension, and if it doesn't have one, it will default the MIME type to something else. Try giving the image an extension and see if that gets you anywhere. If it does, then you'll have to figure out a way of forcing apache to recognize that file as whatever type it is. – digitlworld Feb 24 '11 at 21:03
0

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

BradChesney79
  • 650
  • 7
  • 16