Treehouse

PHP Headers and Popular Mime Types

By on  

Like my Create a Basic Web Service Using PHP, MySQL, XML, and JSON illustrates, even though a file's extension ends in PHP, you can still tell the browser that you're outputting a different content type. Here are a few of the more popular content types used on the internet.

Atom

header('Content-Type: application/atom+xml');

CSS

header('Content-Type: text/css');

Javascript

header('Content-Type: text/javascript');

JPEG Image

header('Content-Type: image/jpeg');

JSON

header('Content-Type: application/json');

PDF

header('Content-Type: application/pdf');

RSS

header('Content-Type: application/rss+xml; charset=ISO-8859-1');

Text (Plain)

header('Content-Type: text/plain');

XML

header('Content-Type: text/xml');

Just because a file ends in .PHP doesn't mean it responds with XHTML -- respond however you'd like!

ydkjs-3.png

Recent Features

  • 5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • 5 More HTML5 APIs You Didnât Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • Create Custom Events in MooTools 1.2

    Javascript has a number of native events like "mouseover," "mouseout", "click", and so on. What if you want to create your own events though? Creating events using MooTools is as easy as it gets. The MooTools JavaScript What's great about creating custom events in MooTools is...

  • CSS Counters

    Counters.  They were a staple of the Geocities / early web scene that many of us "older" developers grew up with;  a feature then, the butt of web jokes now.  CSS has implemented its own type of counter, one more sane and straight-forward than the ole...

Discussion

  1. Great list of mime types.
    Perhaps I’d just be careful about the charset at the RSS example. People could add it blindly and suddenly accented characters would stop working if the rest of the site is using other encodings, like utf-8.

  2. Interesting list. Nothing new, but still nice. BTW Javascript and XML should be application, text is obsolete.

  3. @Matěj Grabovský: Interesting. Can you share your resource for knowing that?

  4. @David Walsh: Well, especially Wikipedia, RFCs (concretely RFC3023) and this thing.

  5. Thank you for sharing Matěj!

  6. You’re welcome, sir!

  7. BTW, It is common to prefix experimental mime types with ‘x-‘ (e.g. application/x-json)

  8. Very usefull stuff, I suggest you ad this one too (exe, zip, rar…)

    header(“Content-Type: application/force-download”);

    BTW, 7 comments, since may 7th… and it’s 7 a.m. here in Morocco

  9. Thanks a lot for this information.

  10. Content-Type or Content-type ?

  11. Have been using just the java, css and htmls. Will do with the rest later.
    Thanks for the list David.
    Pleasure reading.
    ;)

  12. Perfect list! Just what I was looking for. Thanks, Mr. Walsh :-)

  13. nodeset[0];
    $i = $r->new_child(“item”);
    $i->new_child(“title”, $head);
    $i->new_child(“link”, $link);
    $i->new_child(“description”, $comment);

    $out = domxml_dumpmem($doc);
    $file = $arch . “.xml”;
    $f = fopen($file, ‘w’);
    fputs($f, $out);
    fclose($f);

    echo $out;
    ?>

  14. […] PHP sidhuvuden och populära MIME-typer […]

  15. […] PHP Headers and Popular Mime Types […]

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!