<p>The mime type is automatically determined based on the file extension.</p>

<p>There is nothing within Craft which determines the mime type. Detection follows normal server standards, looking at the file extension to figure it out.</p>

<p><strong>Update:</strong></p>

<p>After a little digging, it appears that Craft relies on the Yii <strong><a href="http://www.yiiframework.com/doc/api/1.1/CFileHelper">CFileHelper</a></strong> class to determine the mime type of a file. Reading through the Yii docs gives the impression that several techniques are used to figure out the mime type (including looking at the file extension).</p>

<p>Bottom line... Give your file the right extension, and it will be served up with the correct mime type.</p>

CC BY-SA 3.0
2014-06-15T02:54:25.690Z
2014-06-15T03:04:12.600Z
2014-06-15T03:04:12.600Z
Lindsey D
<p>If you want to explicitly set the mime type from your template, you can do it with Craft's <a href="http://buildwithcraft.com/docs/templating/tags#header">{% header %} tag</a>:</p>

<pre><code>{% header "Content-Type: application/json" %}
</code></pre>

<p>If it is already set via this method, Craft will go with it and not attempt to auto-detect/set the mime-type on it's own.</p>

CC BY-SA 3.0
2014-06-16T12:21:38.590Z
You now have access to moderator tools.
nicael
CC BY-SA 3.0
2014-06-16T12:53:25.783Z
Lindsey totally has the better answer though :)
Brandon Kelly
CC BY-SA 3.0
2014-06-16T17:49:03.797Z
Lol, thanks Brandon! My answer is more relevant to "how does Craft **automatically determine** the mime type", whereas yours is more relevant to "how can I **manually set** the mime type"... Since the original title of this question was a little more ambiguous, I think we both gave solid answers! :)
Lindsey D
CC BY-SA 3.0
2014-06-18T02:51:20.193Z
I think it's good to have both answers since you can both manually set the type and Craft will auto-detect (as a fallback) and yes definitely both solid answers!
Kara
CC BY-SA 4.0
2020-10-29T19:06:55.027Z
This no longer works? Can't seem to override the text/html header.
jishi
CC BY-SA 3.0
2014-06-15T13:14:44.017Z
2014-06-15T20:07:09.757Z
2014-06-15T20:07:09.757Z
Brandon Kelly
<p>For example, if I'd like to serve a file as <code>application/json</code> or <code>application/rss+xml</code></p>

<p>I see in the <a href="http://buildwithcraft.com/docs/templating/rss-feed">RSS Feed documentation</a> that it mentions saving a file with .rss will auto-detect and serve the correct mime-type. </p>

<p>However, how is this detected? Is there a set list that the CMS uses based on the extension or is it derived from the server in some way?</p>

CC BY-SA 3.0
2014-06-15T01:55:51.743Z
I *believe* the server just lets Craft know what response headers to send based on the file it's rendering. I'd put that as an answer but I am not positive, also not sure how to set it with twig nor having much luck finding documentation on that, which leads me to think you can't. I know using a .json file did set the proper content-type for json for me.
Bill Criswell
<templating>
How do I set mime-types for Craft templates?
# How do I set mime-types for Craft templates?

(Asked by: username_0 on 2014-06-15)

For example, if I'd like to serve a file as `application/json` or `application/rss+xml`

I see in the [RSS Feed documentation](http://buildwithcraft.com/docs/templating/rss-feed) that it mentions saving a file with .rss will auto-detect and serve the correct mime-type.

However, how is this detected? Is there a set list that the CMS uses based on the extension or is it derived from the server in some way?

**Tags:** templating

> **Comments:**
> **username_1**: I *believe* the server just lets Craft know what response headers to send based on the file it's rendering. I'd put that as an answer but I am not positive, also not sure how to set it with twig nor having much luck finding documentation on that, which leads me to think you can't. I know using a .json file did set the proper content-type for json for me.  

---

## 2 answers

### Answer by username_3 on 2014-06-15. Score: 18 | Accepted answer

If you want to explicitly set the mime type from your template, you can do it with Craft's {% header %} tag:
{% header "Content-Type: application/json" %}

If it is already set via this method, Craft will go with it and not attempt to auto-detect/set the mime-type on it's own.

> **Comments:**
> **username_4**: You now have access to moderator tools.  
> **username_3**: Lindsey totally has the better answer though :)  
> **username_2**: Lol, thanks Brandon! My answer is more relevant to "how does Craft **automatically determine** the mime type", whereas yours is more relevant to "how can I **manually set** the mime type"... Since the original title of this question was a little more ambiguous, I think we both gave solid answers! :)  
> **username_0**: I think it's good to have both answers since you can both manually set the type and Craft will auto-detect (as a fallback) and yes definitely both solid answers!  
> **username_5**: This no longer works? Can't seem to override the text/html header.  

---

### Answer by username_2 on 2014-06-15. Score: 9

The mime type is automatically determined based on the file extension.

There is nothing within Craft which determines the mime type. Detection follows normal server standards, looking at the file extension to figure it out.

**Update:**

After a little digging, it appears that Craft relies on the Yii **[CFileHelper](http://www.yiiframework.com/doc/api/1.1/CFileHelper)** class to determine the mime type of a file. Reading through the Yii docs gives the impression that several techniques are used to figure out the mime type (including looking at the file extension).

Bottom line... Give your file the right extension, and it will be served up with the correct mime type.


---

