Heim  >  Artikel  >  Backend-Entwicklung  >  PHP实现简单的文件下载通用方法

PHP实现简单的文件下载通用方法

WBOY
WBOYOriginal
2016-07-25 08:45:16727Durchsuche
  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


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn