PHP Headers and Popular Mime Types

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!


Comments

  1. Bruno Lustosa

    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. Matěj Grabovský

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

  3. David Walsh

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

  4. Matěj Grabovský

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

  5. David Walsh

    Thank you for sharing Matěj!

  6. Matěj Grabovský

    You’re welcome, sir!

  7. Eric Wendelin

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

  8. kadimi

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

    Thanks a lot for this information.

  10. francis

    Content-Type or Content-type ?


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: