1

If an image file name does not reflect its correct file type(e.g stored with .pdf extension), is it safe to use it in HTML? Will the browser decide the correct type of the image? Will mobile browsers be able to deduce correct file type?

I have tested it with google chrome, it is working, but Is it guaranteed to run on all reasonable browsers?

UPDATE: I can't rename them to correct extensions, since they will be uploaded by users and then shown again.

  • probably a silly question but why don't you just rename the files with the correct file suffix – olly_uk Feb 27 '13 at 14:34
  • 2
    Web browsers generally don't care about the file extension, they care about the Content Type: http://en.wikipedia.org/wiki/MIME_type Whether or not it's _guaranteed_ depends on the browser, but in general the Content Type is how a browser determines what to do with a stream of data. Keep in mind that, in HTTP, there is no such thing as a "file." And, thus, an "extension" is meaningless. A request and a response contains headers and data. If the data is meant to be treated as a "file" then the headers should indicate as such. – David Feb 27 '13 at 14:34
  • @olly_uk, images are uploaded by users, I want to show the images to them again –  Feb 27 '13 at 14:37
  • @David thnx, It looks I can use *the wrong ext files* as they are in html. –  Feb 27 '13 at 14:38

1 Answers1

3

Will mobile browsers be able to deduce correct file type?

Browsers don't usually deduce file types (there are exceptions, notably in IE—resulting in text files discussing HTML being treated as HTML and IIS servers sending text/plain content-types for HTML documents without their owners noticing—but they shouldn't be the primary concern).

Instead, browsers determine the type of data by examining the HTTP Content-Type Response header. By default, most servers will set this based on the file extension of the file they are reading from the filesystem to serve to the client.

You can override this, but doing so is fiddly and could cause problems if people save a file before opening it from their local file system (because it will have the wrong extension and their OS will associate it with the wrong application).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335