Home  >  Article  >  Backend Development  >  PHP code to force download of PDF files

PHP code to force download of PDF files

WBOY
WBOYOriginal
2016-07-25 08:54:501025browse
  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. }

Copy code

Instructions: The above implements a function forceDownload(), and then by calling this function, PHP can force download files.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn