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