PHP forces file download (avoid files or images being opened directly in the browser)

WBOY
Release: 2016-07-25 09:00:51
Original
1402 people have browsed it
  1. /**
  2. * The FLEA_Helper_SendFile class is used to send files to the browser
  3. *
  4. * Using FLEA_Helper_SendFile, applications can save important files in
  5. * locations that are inaccessible to the browser. The file contents are then sent to the browser through the program.
  6. * @site http://bbs.it-home.org
  7. */
  8. class FLEA_Helper_SendFile
  9. {
  10. /**
  11. * Send the file content to the browser
  12. *
  13. * @param string $serverPath The path of the file on the server (absolute or relative path)
  14. * @param string $filename The file name sent to the browser (do not use Chinese if possible)
  15. * @param string $mimeType indicates the file type
  16. */
  17. function sendFile($serverPath, $filename, $mimeType = 'application/octet- stream')
  18. {
  19. header("Content-Type: {$mimeType}");
  20. $filename = '"' . htmlspecialchars($filename) . '"';
  21. $filesize = filesize($serverPath);
  22. $ charset = FLEA::getAppInf('responseCharset');//According to the actual file encoding type, such as utf-8, gbk
  23. header("Content-Disposition: attachment; filename={$filename}; charset={$charset}" );
  24. header('Pragma: cache');
  25. header('Cache-Control: public, must-revalidate, max-age=0');
  26. header("Content-Length: {$filesize}");
  27. readfile($serverPath);
  28. exit;
  29. }
  30. }
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template