设为首页 加入收藏

TOP

php实现大文件断点续传下载实例(二)
2019-10-09 19:56:46 】 浏览:72
Tags:php 实现 文件 断点 下载 实例
_magic_quotes_runtime(0);
106 // init 107 $lastModified = gmdate('D, d M Y H:i:s', filemtime($this->filePath)) . ' GMT'; 108 $etag = sprintf('w/"%s:%s"', md5($lastModified), $this->fileSize); 109 $ranges = $this->getRange(); 110 // headers 111 header(sprintf('Last-Modified: %s', $lastModified)); 112 header(sprintf('ETag: %s', $etag)); 113 header(sprintf('Content-Type: %s', $this->mimeType)); 114 $disposition = 'attachment'; 115 if (strpos($this->mimeType, 'image/') !== FALSE) { 116 $disposition = 'inline'; 117 } 118 header(sprintf('Content-Disposition: %s; filename="%s"', $disposition, basename($this->filePath))); 119 120 if ($ranges != null) { 121 if ($this->isLog) { 122 $this->log(json_encode($ranges) . ' ' . $_SERVER['HTTP_RANGE']); 123 } 124 header('HTTP/1.1 206 Partial Content'); 125 header('Accept-Ranges: bytes'); 126 header(sprintf('Content-Length: %u', $ranges['end'] - $ranges['start'])); 127 header(sprintf('Content-Range: bytes %s-%s/%s', $ranges['start'], $ranges['end'], $this->fileSize)); 128 // 129 fseek($fileHande, sprintf('%u', $ranges['start'])); 130 } else { 131 header("HTTP/1.1 200 OK"); 132 header(sprintf('Content-Length: %s', $this->fileSize)); 133 } 134 // read file 135 $lastSize = 0; 136 while (!feof($fileHande) && !connection_aborted()) { 137 $lastSize = sprintf("%u", bcsub($this->fileSize, sprintf("%u", ftell($fileHande)))); 138 if (bccomp($lastSize, self::BUFF_SIZE) > 0) { 139 $lastSize = self::BUFF_SIZE; 140 } 141 echo fread($fileHande, $lastSize); 142 ob_flush(); 143 flush(); 144 } 145 set_magic_quotes_runtime($magicQuotes); 146 ob_end_flush(); 147 } 148 if ($fileHande != null) { 149 fclose($fileHande); 150 } 151 } 152 153 /** 154 * 设置记录 155 * @param <Boolean> $isLog 是否记录 156 */ 157 public function setIsLog($isLog = true) { 158 $this->isLog = $isLog; 159 } 160 161 /** 162 * 记录 163 * @param <String> $msg 记录信息 164 */ 165 private function log($msg) { 166 try { 167 $handle = fopen('transfer_log.txt', 'a'); 168 fwrite($handle, sprintf('%s : %s' . PHP_EOL, date('Y-m-d H:i:s'), $msg)); 169 fclose($handle); 170 } catch (Exception $e) { 171 // null; 172 } 173 } 174 175 }


本文转自:https://www.sucaihuo.com/php/277.html 转载请注明出处!

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[视频教程] 使用composer安装使用.. 下一篇php配置文件说明

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目