Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top