欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php中利用header设置content-type和常见文件类型的content-type

在PHP中可以通过header函数来发送头信息,还可以设置文件的content-type,下面整理了一些常见文件类型对于的content-type值。

部分header头参考:http://www.lai18.com/content/433566.html

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


php中利用header设置content-type和常见文件类型的content-type
在PHP中可以通过header函数来发送头信息,还可以设置文件的content-type,下面整理了一些常见文件类型对于的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: <a href="http://www.example.org/" );'="" target="_blank">http://www.example.org/');</a>   
  42. //文件延迟转向:  
  43. header('Refresh: 10; url=http://www.example.org/');  
  44. print 'You will be redirected in 10 seconds';  
  45.   
  46. //当然,也可以使用html语法实现  
  47. // <meta http-equiv="refresh" content="10;http://www.example.org/ />  
  48.   
  49. // override X-Powered-By: PHP:  
  50. header('X-Powered-By: PHP/4.4.0');  
  51. header('X-Powered-By: Brain/0.6b');  
  52.   
  53. //文档语言  
  54. header('Content-language: en');  
  55.   
  56. //告诉浏览器最后一次修改时间  
  57. $time = time() - 60; // or filemtime($fn), etc  
  58. header('Last-Modified: '.gmdate('D, d M Y H:i:s'$time).' GMT');  
  59.   
  60. //告诉浏览器文档内容没有发生改变  
  61. header('HTTP/1.1 304 Not Modified');  
  62.   
  63. //设置内容长度  
  64. header('Content-Length: 1234');  
  65.   
  66. //设置为一个下载类型  
  67. header('Content-Type: application/octet-stream');  
  68. header('Content-Disposition: attachment; filename="example.zip"');  
  69. header('Content-Transfer-Encoding: binary');  
  70. // load the file to send:  
  71. readfile('example.zip');  
  72.   
  73. // 对当前文档禁用缓存  
  74. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');  
  75. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past  
  76. header('Pragma: no-cache');  
  77.   
  78. //设置内容类型:  
  79. header('Content-Type: text/html; charset=iso-8859-1');  
  80. header('Content-Type: text/html; charset=utf-8');  
  81. header('Content-Type: text/plain'); //纯文本格式  
  82. header('Content-Type: image/jpeg'); //JPG***  
  83. header('Content-Type: application/zip'); // ZIP文件  
  84. header('Content-Type: application/pdf'); // PDF文件  
  85. header('Content-Type: audio/mpeg'); // 音频文件  
  86. header('Content-Type: application/x-shockw**e-flash'); //Flash动画  
  87.   
  88. //显示登陆对话框  
  89. header('HTTP/1.1 401 Unauthorized');  
  90. header('WWW-Authenticate: Basic realm="Top Secret"');  
  91. print 'Text that will be displayed if the user hits cancel or ';  
  92. print 'enters wrong login data';  

 
普通分类: