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) 2015,, All Rights Reserved.
   $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));
   } else {
      print("<html><body>Bad request.</body></html>");

Ideas used in this script:

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: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

Controlling HTTP Response Header Lines in PHP Scripts

 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

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials


 PDF Printing Version