龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php设定http头输出非html内容

时间:2015-01-10 02:21来源:网络整理 作者:网络 点击:
分享到:
php设定http头输出非html内容 [代码片段(56行)]
<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)

精彩图集

赞助商链接