_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 转载请注明出处!
|