PHP强制下载PDF文件的代码

原创
2016-07-25 08:54:50 774浏览
  1. forceDownload("pdfdemo.pdf");

  2. function forceDownload($filename) {
  3. if (false == file_exists($filename)) {

  4. return false;
  5. }
  6. // http headers

  7. header('Content-Type: application-x/force-download');
  8. header('Content-Disposition: attachment; filename="' . basename($filename) .'"');
  9. header('Content-length: ' . filesize($filename));
  10. // for IE6

  11. if (false === strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6')) {
  12. header('Cache-Control: no-cache, must-revalidate');
  13. }
  14. header('Pragma: no-cache');
  15. // read file content and output

  16. return readfile($filename);;
  17. }
复制代码

说明: 以上实现一个函数forceDownload(),然后通过调用该函数即可实现php强制下载文件。



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。