Sending Content Types Other Than HTML : header « HTML « PHP






Sending Content Types Other Than HTML

 
<html>
</head>
<body>
  <div align="center">
    <img src="index.php" alt="" title="" style="border: none;" />
  </div>
</body>
</html>
<?php
  $path = "myImage.jpg";
  try {
    if (is_file ($path)){
      if ($file = fopen($path, 'rb')) {
        while(!feof($file) and (connection_status()==0)) {
          $f .= fread($file, 1024*8);
        }
        fclose($file);
      }
      header ("Content-type: image/jpeg");
      print $f;
    } else {
      throw new exception ("Sorry, file path is not valid.");
    }
  } catch (exception $e){
    $animage = imagecreate (500, 500);
    $red = imagecolorallocate ($animage, 255, 0, 0);
    $white = imagecolorallocate ($animage, 255, 255, 255);
    imagefilledrectangle ($animage, 0, 0, 500, 500, $white);
    imagestring ($animage, 4, ((500 - (strlen($e->getmessage()) * imagefontwidth(4))) / 2), 5, $e->getmessage(), $red);
    imagejpeg ($animage);
    header ("Content-type: image/jpeg");
    imagedestroy ($animage);
  }
  
?>

Common File Format Content Types

Content Type                      Application

application/pdf                   Adobe Portable Document Format (PDF) types

application/msword                Microsoft Word documents

application/excel                 Microsoft Excel documents

image/gif                         GIF images

image/png                         PNG images

application/octet-stream          Zip files

text/plain                        Plain text (text files)
  
  








Related examples in the same category

1.Changing the page type to CSV
2.Changing the page type to XML
3.Setting a Cookie Using the header() Function
4.Logged In with cookie
5.Forcing File "Save As" Downloads
6.Good and bad Location headers
7.If a User Is Not Logged in, the Login Form Is Loaded
8.Mime types
9.Using header() to Send Raw Headers
10.Setting character encoding
11.Sending an XML response
12.Reading Queued Headers
13.Redirect header
14.Redirecting with query string variables