php的header函数之设置content-type

时间:2023-12-17 23:47:02
  1. //定义编码
  2. header( 'Content-Type:text/html;charset=utf-8 ');
  3. //Atom
  4. header('Content-type: application/atom+xml');
  5. //CSS
  6. header('Content-type: text/css');
  7. //Javascript
  8. header('Content-type: text/javascript');
  9. //JPEG Image
  10. header('Content-type: image/jpeg');
  11. //JSON
  12. header('Content-type: application/json');
  13. //PDF
  14. header('Content-type: application/pdf');
  15. //RSS
  16. header('Content-Type: application/rss+xml; charset=ISO-8859-1');
  17. //Text (Plain)
  18. header('Content-type: text/plain');
  19. //XML
  20. header('Content-type: text/xml');
  21. // ok
  22. header('HTTP/1.1 200 OK');
  23. //设置一个404头:
  24. header('HTTP/1.1 404 Not Found');
  25. //设置地址被永久的重定向
  26. header('HTTP/1.1 301 Moved Permanently');
  27. //转到一个新地址
  28. header('Location: http://www.example.org/');
  29. //文件延迟转向:
  30. header('Refresh: 10; url=http://www.example.org/');
  31. print 'You will be redirected in 10 seconds';
  32. //当然,也可以使用html语法实现
  33. // <meta http-equiv="refresh" content="10;http://www.example.org/ />
  34. // override X-Powered-By: PHP:
  35. header('X-Powered-By: PHP/4.4.0');
  36. header('X-Powered-By: Brain/0.6b');
  37. //文档语言
  38. header('Content-language: en');
  39. //告诉浏览器最后一次修改时间
  40. $time = time() - 60; // or filemtime($fn), etc
  41. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
  42. //告诉浏览器文档内容没有发生改变
  43. header('HTTP/1.1 304 Not Modified');
  44. //设置内容长度
  45. header('Content-Length: 1234');
  46. //设置为一个下载类型
  47. header('Content-Type: application/octet-stream');
  48. header('Content-Disposition: attachment; filename="example.zip"');
  49. header('Content-Transfer-Encoding: binary');
  50. // load the file to send:
  51. readfile('example.zip');
  52. // 对当前文档禁用缓存
  53. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  54. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  55. header('Pragma: no-cache');
  56. //设置内容类型:
  57. header('Content-Type: text/html; charset=iso-8859-1');
  58. header('Content-Type: text/html; charset=utf-8');
  59. header('Content-Type: text/plain'); //纯文本格式
  60. header('Content-Type: image/jpeg'); //JPG***
  61. header('Content-Type: application/zip'); // ZIP文件
  62. header('Content-Type: application/pdf'); // PDF文件
  63. header('Content-Type: audio/mpeg'); // 音频文件
  64. header('Content-Type: application/x-shockw**e-flash'); //Flash动画
  65. //显示登陆对话框
  66. header('HTTP/1.1 401 Unauthorized');
  67. header('WWW-Authenticate: Basic realm="Top Secret"');
  68. print 'Text that will be displayed if the user hits cancel or ';
  69. print 'enters wrong login data';
 Content-type 的说明

 $mimetypes = array(
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil', 'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh', 'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk',
);

The following table documents the HTTP MIME types that are available when working with Office 2007 documents:

Ext

MIME Type

.doc

application/msword

.dot

application/msword

.docx

application/vnd.openxmlformats-officedocument.wordprocessingml.document

.dotx

application/vnd.openxmlformats-officedocument.wordprocessingml.template

.docm

application/vnd.ms-word.document.macroEnabled.12

.dotm

application/vnd.ms-word.template.macroEnabled.12

.xls

application/vnd.ms-excel

.xlt

application/vnd.ms-excel

.xla

application/vnd.ms-excel

.xlsx

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

.xltx

application/vnd.openxmlformats-officedocument.spreadsheetml.template

.xlsm

application/vnd.ms-excel.sheet.macroEnabled.12

.xltm

application/vnd.ms-excel.template.macroEnabled.12

.xlam

application/vnd.ms-excel.addin.macroEnabled.12

.xlsb

application/vnd.ms-excel.sheet.binary.macroEnabled.12

.ppt

application/vnd.ms-powerpoint

.pot

application/vnd.ms-powerpoint

.pps

application/vnd.ms-powerpoint

.ppa

application/vnd.ms-powerpoint

.pptx

application/vnd.openxmlformats-officedocument.presentationml.presentation

.potx

application/vnd.openxmlformats-officedocument.presentationml.template

.ppsx

application/vnd.openxmlformats-officedocument.presentationml.slideshow

.ppam

application/vnd.ms-powerpoint.addin.macroEnabled.12

.pptm

application/vnd.ms-powerpoint.presentation.macroEnabled.12

.potm

application/vnd.ms-powerpoint.presentation.macroEnabled.12

.ppsm

application/vnd.ms-powerpoint.slideshow.macroEnabled.12