Chinaunix首页 | 论坛 | 博客
  • 博客访问: 448466
  • 博文数量: 141
  • 博客积分: 211
  • 博客等级: 入伍新兵
  • 技术积分: 1049
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-17 16:25
个人简介

如此经年,望尽千帆。

文章分类

全部博文(141)

文章存档

2014年(73)

2013年(65)

2012年(3)

我的朋友

分类: PHP

2014-06-23 05:56:31

当需要下载一个PDF文件时,如果不经处理会直接在浏览器里打开PDF文件,然后再需要通过另存为才能保存下载文件。
本文将通过PHP来实现直接下载PDF文件。 

实现原理:只需要修改页面HTTP头,把Content-Type设置为force-download,问题即可解决。 

请看代码: 

  1. forceDownload("pdfdemo.pdf");   
  2. function forceDownload($filename) {   
  3.   
  4. if (false == file_exists($filename)) {   
  5. return false;   
  6. }   
  7.   
  8. // http headers   
  9. header('Content-Type: application-x/force-download');   
  10. header('Content-Disposition: attachment; filename="' . basename($filename) .'"');   
  11. header('Content-length: ' . filesize($filename));   
  12.   
  13. // for IE6   
  14. if (false === strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6')) {   
  15. header('Cache-Control: no-cache, must-revalidate');   
  16. }   
  17. header('Pragma: no-cache');   
  18.   
  19. // read file content and output   
  20. return readfile($filename);;   
  21. }   

您可能感兴趣的文章:








阅读(1502) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~