当前路径:vendor/topthink/framework/src/think/response/File.php <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- declare (strict_types = 1); namespace think\response; use think\Exception; use think\Response; /** * File Response */ class File extends Response { protected $expire = 360; protected $name; protected $mimeType; protected $isContent = false; protected $force = true; public function __construct($data = '', int $code = 200) { $this->init($data, $code); } /** * 澶勭悊鏁版嵁 * @access protected * @param mixed $data 瑕佸鐞嗙殑鏁版嵁 * @return mixed * @throws \Exception */ protected function output($data) { if (!$this->isContent && !is_file($data)) { throw new Exception('file not exists:' . $data); } ob_end_clean(); if (!empty($this->name)) { $name = $this->name; } else { $name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : ''; } if ($this->isContent) { $mimeType = $this->mimeType; $size = strlen($data); } else { $mimeType = $this->getMimeType($data); $size = filesize($data); } $this->header['Pragma'] = 'public'; $this->header['Content-Type'] = $mimeType ?: 'application/octet-stream'; $this->header['Cache-control'] = 'max-age=' . $this->expire; $this->header['Content-Disposition'] = ($this->force ? 'attachment; ' : '') . 'filename="' . $name . '"'; $this->header['Content-Length'] = $size; $this->header['Content-Transfer-Encoding'] = 'binary'; $this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT'; $this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT'); return $this->isContent ? $data : file_get_contents($data); } /** * 璁剧疆鏄惁涓哄唴瀹?蹇呴』閰嶅悎mimeType鏂规硶浣跨敤 * @access public * @param bool $content * @return $this */ public function isContent(bool $content = true) { $this->isContent = $content; return $this; } /** * 璁剧疆鏈夋晥鏈? * @access public * @param integer $expire 鏈夋晥鏈? * @return $this */ public function expire(int $expire) { $this->expire = $expire; return $this; } /** * 璁剧疆鏂囦欢绫诲瀷 * @access public * @param string $filename 鏂囦欢鍚? * @return $this */ public function mimeType(string $mimeType) { $this->mimeType = $mimeType; return $this; } /** * 璁剧疆鏂囦欢寮哄埗涓嬭浇 * @access public * @param bool $force 寮哄埗娴忚鍣ㄤ笅杞? * @return $this */ public function force(bool $force) { $this->force = $force; return $this; } /** * 鑾峰彇鏂囦欢绫诲瀷淇℃伅 * @access public * @param string $filename 鏂囦欢鍚? * @return string */ protected function getMimeType(string $filename): string { if (!empty($this->mimeType)) { return $this->mimeType; } $finfo = finfo_open(FILEINFO_MIME_TYPE); return finfo_file($finfo, $filename); } /** * 璁剧疆涓嬭浇鏂囦欢鐨勬樉绀哄悕绉? * @access public * @param string $filename 鏂囦欢鍚? * @param bool $extension 鍚庣紑鑷姩璇嗗埆 * @return $this */ public function name(string $filename, bool $extension = true) { $this->name = $filename; if ($extension && false === strpos($filename, '.')) { $this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION); } return $this; } }
相关源码
- 可旋转的彩色立方体C#源代码2021-10-29
- 在线考试系统2021-10-15
- EduSoho开源网校系统源码2019-06-27
- 仿拼多多小程序商城源码2019-06-06
- PHP5网站运行监测系统源码2017-04-14
关于我们 | 顾问团队 | 发展历程 | 联系我们 | 源码上传
联系电话(Tel):4008-010-151(免长途)
地址:北京市海淀区大恒科技大厦五层 邮编:100080
Floor 5th,Daheng Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2023. 京ICP备09089570号 | 京公网安备11010702000869号
联系电话(Tel):4008-010-151(免长途)
地址:北京市海淀区大恒科技大厦五层 邮编:100080
Floor 5th,Daheng Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2023. 京ICP备09089570号 | 京公网安备11010702000869号