php header方法 修改传输头

来源:互联网 发布:利驰软件电气教程 编辑:程序博客网 时间:2024/06/05 05:32

修改文本编码

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. header("Content-Type: text/html;charset=utf-8");  


重定向

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // header("Location:a.php");  
  2. header("Location:http://www.126.com");  


多少秒后跳转

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. header("Refresh:5;url='http://www.126.com'");  


缓存

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //不启用缓存 多个配置 适配不同的浏览器  
  2. header("Expires:-1");  
  3. header("Cache-Control:no-cache");  
  4. header("Pragma:no-cache");  


文件下载

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. //在此页面中不能调用echo等方法在页面显示内容,否则下载的内容就是错误的  
  3. $str = "我的.jpg";  
  4. //中文的文件名 php文件系统不认,须转换  
  5. $str=iconv('UTF-8','GB2312',$str);  
  6. if(!file_exists($str)){  
  7.     echo "找不到文件:".$str;  
  8.     return;  
  9. }  
  10. $fp = fopen($str"r");  
  11. $file_size = filesize($str);  
  12. while (! feof($fp)) {  
  13.     $read = fread($fp, 1024);  
  14.     echo $read;  
  15. }  
  16. fclose($fp);  
  17. // 说明返回的是文件  
  18. header("Content-Type:application/octet-stream");  
  19. // 按字节大小返回  
  20. header("Accept-Range:bytes");  
  21. // 文件有多大  
  22. header("Content-Length:" . $file_size);  
  23. // 文件名 弹出框的名称  
  24. header("Content-Disposition:attachment; filename=" . $str);  
  25.   
  26. ?>  
0 0
原创粉丝点击