No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Download.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\response;
  12. use think\Exception;
  13. use think\Response;
  14. class Download extends Response
  15. {
  16. protected $expire = 360;
  17. protected $name;
  18. protected $mimeType;
  19. protected $isContent = false;
  20. protected $openinBrowser = false;
  21. /**
  22. * 处理数据
  23. * @access protected
  24. * @param mixed $data 要处理的数据
  25. * @return mixed
  26. * @throws \Exception
  27. */
  28. protected function output($data)
  29. {
  30. if (!$this->isContent && !is_file($data)) {
  31. throw new Exception('file not exists:' . $data);
  32. }
  33. while (ob_get_level() > 0) {
  34. ob_end_clean();
  35. }
  36. if (!empty($this->name)) {
  37. $name = $this->name;
  38. } else {
  39. $name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : '';
  40. }
  41. if ($this->isContent) {
  42. $mimeType = $this->mimeType;
  43. $size = strlen($data);
  44. } else {
  45. $mimeType = $this->getMimeType($data);
  46. $size = filesize($data);
  47. }
  48. $this->header['Pragma'] = 'public';
  49. $this->header['Content-Type'] = $mimeType ?: 'application/octet-stream';
  50. $this->header['Cache-control'] = 'max-age=' . $this->expire;
  51. $this->header['Content-Disposition'] = $this->openinBrowser ? 'inline' : 'attachment; filename="' . $name . '"';
  52. $this->header['Content-Length'] = $size;
  53. $this->header['Content-Transfer-Encoding'] = 'binary';
  54. $this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT';
  55. $this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT');
  56. $data = $this->isContent ? $data : file_get_contents($data);
  57. return $data;
  58. }
  59. /**
  60. * 设置是否为内容 必须配合mimeType方法使用
  61. * @access public
  62. * @param bool $content
  63. * @return $this
  64. */
  65. public function isContent($content = true)
  66. {
  67. $this->isContent = $content;
  68. return $this;
  69. }
  70. /**
  71. * 设置有效期
  72. * @access public
  73. * @param integer $expire 有效期
  74. * @return $this
  75. */
  76. public function expire($expire)
  77. {
  78. $this->expire = $expire;
  79. return $this;
  80. }
  81. /**
  82. * 设置文件类型
  83. * @access public
  84. * @param string $filename 文件名
  85. * @return $this
  86. */
  87. public function mimeType($mimeType)
  88. {
  89. $this->mimeType = $mimeType;
  90. return $this;
  91. }
  92. /**
  93. * 获取文件类型信息
  94. * @access public
  95. * @param string $filename 文件名
  96. * @return string
  97. */
  98. protected function getMimeType($filename)
  99. {
  100. if (!empty($this->mimeType)) {
  101. return $this->mimeType;
  102. }
  103. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  104. return finfo_file($finfo, $filename);
  105. }
  106. /**
  107. * 设置下载文件的显示名称
  108. * @access public
  109. * @param string $filename 文件名
  110. * @param bool $extension 后缀自动识别
  111. * @return $this
  112. */
  113. public function name($filename, $extension = true)
  114. {
  115. $this->name = $filename;
  116. if ($extension && false === strpos($filename, '.')) {
  117. $this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION);
  118. }
  119. return $this;
  120. }
  121. /**
  122. * 设置是否在浏览器中显示文件
  123. * @access public
  124. * @param bool $openinBrowser 是否在浏览器中显示文件
  125. * @return $this
  126. */
  127. public function openinBrowser($openinBrowser)
  128. {
  129. $this->openinBrowser = $openinBrowser;
  130. return $this;
  131. }
  132. }