PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Content-Type: - Generating Non-HTML Response Body

This section provides a tutorial example on how to change the 'Content-Type:' header line in a HTTP response to tell the browser to handle the response entity body as different document types, like ext/html, image/gif, application/pdf, application/msword, etc.

Sometimes, you may want to send back information in the response entity body that is not in the HTML format, for example, a PDF document, or a MS Word Document. In this case, we have to set Content-Type, Content-Length and other header lines carefully to provide correct information about the entity body for the client program. Here is a sample PHP script that shows you how to set header lines for different types of data in the entity body.

<?php #GetFile.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
   $ok = array_key_exists('QUERY_STRING', $_SERVER);
   if ($ok) {
      $p = $_SERVER['QUERY_STRING'];
      $ok = strlen($p)>0 && file_exists($p);
   }
   if ($ok) {
      if (strpos($p,".html")!=false) {
         header("Content-Type: text/html");
      } else if (strpos($p,".gif")!=false) {
         header("Content-Type: image/gif");
      } else if (strpos($p,".pdf")!=false) {
         header("Content-Type: application/pdf");
      } else if (strpos($p,".doc")!=false) {
         header("Content-Type: application/msword");
      } else {
         $ok = false;
      }
   }
   if ($ok) {
      header("Content-Length: ".filesize($p));
      readfile($p);
   } else {
      print("<html><body>Bad request.</body></html>");
   }
?>

Ideas used in this script:

  • The objective is to send back the content of the requested file in entity body, and set the Content-Type and Content-Length header lines correctly.
  • The requested file name is given in the query string of the HTTP request. array_key_exists() is used to make sure the query string is provided to avoid array index error.
  • The extension of the requested file name is checked to determine the Content-Type header line. strpos() is used as a Boolean value to check the file name extension.
  • Then the requested file size is checked to set the Content-Length header line.
  • Then the requested file is outputted to the HTTP response with a single function readfile(). This function name is a little bit misleading, but the function is very useful.
  • If any thing goes wrong, a HTML entity is returned with an error message. A Boolean variable $ok is used to monitor the error condition.

Now let's see how this page works. Assuming I have GetFile.php, hello.html, dot.gif, hello.pdf, and hello.doc copied to c:\inetpub\wwwroot directory. Of course, my IIS is configured to the PHP engine.

1. Use IE (Internet Explorer) to request: http://localhost/GetFile.php?hello.html, you should see the hello message properly displayed as a HTML document.

2. Use IE to request: http://localhost/GetFile.php?dot.gif, you should see a tiny dot displayed as an image.

3. Use IE to request: http://localhost/GetFile.php?hello.pdf, you should see IE calling Adobe Reader to display the hello message as a PDF document.

4. Use IE to request: http://localhost/GetFile.php?hello.doc, you should see IE calling MS Word to display the hello message as a MS Word document.

5. Use IE to request: http://localhost/GetFile.php?any.file, you should see IE displaying an error message. The reason, of course, is that the requested file doesn't exist.

6. Use IE to request: http://localhost/GetFile.php you should see IE displaying an error message. The reason is that there is no query string in the HTTP request.

Last update: 2005.

Sections in This Chapter

What Is an HTTP Response?

HTTP Response Header Lines

header() - Inserting a Raw Header Lines

HttpRequestGet.php - Viewing Header Lines

Response Header Lines of Static Files

HttpHeaderLines.php - Examples of Inserting Header Lines

Location: - Forcing the Browser to Redirect to Another URL

Content-Type: - Generating Non-HTML Response Body

Content-Disposition: - Sending Files for Downloading

Dr. Herong Yang, updated in 2009
Content-Type: - Generating Non-HTML Response Body