分享

php的header函数之设置content

 地下室1024 2015-04-18

设置常用的content-type:

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

下载xlsx文件

  1. $filename = rtrim($_SERVER['DOCUMENT_ROOT'],'/').'/app/files/payment_status.csv';  
  2. header('Content-Disposition: attachment; filename=payment_status.xlsx');  
  3. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  
  4. header('Content-Length: ' . filesize($filename));  
  5. header('Content-Transfer-Encoding: binary');  
  6. header('Cache-Control: must-revalidate');  
  7. header('Pragma: public');  
  8. readfile($filename);  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多