0
0

Download.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ob_end_clean();
  34. if (!empty($this->name)) {
  35. $name = $this->name;
  36. } else {
  37. $name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : '';
  38. }
  39. if ($this->isContent) {
  40. $mimeType = $this->mimeType;
  41. $size = strlen($data);
  42. } else {
  43. $mimeType = $this->getMimeType($data);
  44. $size = filesize($data);
  45. }
  46. $this->header['Pragma'] = 'public';
  47. $this->header['Content-Type'] = $mimeType ?: 'application/octet-stream';
  48. $this->header['Cache-control'] = 'max-age=' . $this->expire;
  49. $this->header['Content-Disposition'] = $this->openinBrowser ? 'inline' : 'attachment; filename="' . $name . '"';
  50. $this->header['Content-Length'] = $size;
  51. $this->header['Content-Transfer-Encoding'] = 'binary';
  52. $this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT';
  53. $this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT');
  54. $data = $this->isContent ? $data : file_get_contents($data);
  55. return $data;
  56. }
  57. /**
  58. * 设置是否为内容 必须配合mimeType方法使用
  59. * @access public
  60. * @param bool $content
  61. * @return $this
  62. */
  63. public function isContent($content = true)
  64. {
  65. $this->isContent = $content;
  66. return $this;
  67. }
  68. /**
  69. * 设置有效期
  70. * @access public
  71. * @param integer $expire 有效期
  72. * @return $this
  73. */
  74. public function expire($expire)
  75. {
  76. $this->expire = $expire;
  77. return $this;
  78. }
  79. /**
  80. * 设置文件类型
  81. * @access public
  82. * @param string $filename 文件名
  83. * @return $this
  84. */
  85. public function mimeType($mimeType)
  86. {
  87. $this->mimeType = $mimeType;
  88. return $this;
  89. }
  90. /**
  91. * 获取文件类型信息
  92. * @access public
  93. * @param string $filename 文件名
  94. * @return string
  95. */
  96. protected function getMimeType($filename)
  97. {
  98. if (!empty($this->mimeType)) {
  99. return $this->mimeType;
  100. }
  101. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  102. return finfo_file($finfo, $filename);
  103. }
  104. /**
  105. * 设置下载文件的显示名称
  106. * @access public
  107. * @param string $filename 文件名
  108. * @param bool $extension 后缀自动识别
  109. * @return $this
  110. */
  111. public function name($filename, $extension = true)
  112. {
  113. $this->name = $filename;
  114. if ($extension && false === strpos($filename, '.')) {
  115. $this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION);
  116. }
  117. return $this;
  118. }
  119. /**
  120. * 设置是否在浏览器中显示文件
  121. * @access public
  122. * @param bool $openinBrowser 是否在浏览器中显示文件
  123. * @return $this
  124. */
  125. public function openinBrowser($openinBrowser) {
  126. $this->openinBrowser = $openinBrowser;
  127. return $this;
  128. }
  129. }