Home  >  Article  >  Backend Development  >  PHP implements a simple universal method for file downloading

PHP implements a simple universal method for file downloading

WBOY
WBOYOriginal
2016-07-25 08:45:16763browse
  1. function download_file($file){
  2. if(is_file($file)){
  3. $length = filesize($file);
  4. $type = mime_content_type($file);
  5. $showname = ltrim(strrchr($file,'/'),'/');
  6. header("Content-Description: File Transfer");
  7. header('Content-type: ' . $type);
  8. header('Content-Length:' . $length);
  9. if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
  10. header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
  11. } else {
  12. header('Content-Disposition: attachment; filename="' . $showname . '"');
  13. }
  14. readfile($file);
  15. exit;
  16. } else {
  17. exit('文件已被删除!');
  18. }
  19. }
复制代码

PHP


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