admin 管理员组文章数量: 887021
2024年2月25日发(作者:excel表格数据汇总)
if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_SERVER['HTTP_RANGE']) && $range =stristr(trim($_SERVER['HTTP_RANGE']), 'bytes=')) {$range = substr($range, 6);$ranges = explode(',', $range);$t = count($ranges);}header("Accept-Ranges: bytes");header("Content-Type: application/octet-stream");header("Content-Transfer-Encoding: binary");header(sprintf('Content-Disposition: attachment; filename="%s"', $this->name));if ($t > 0) {header("HTTP/1.1 206 Partial content");$t === 1 ? $this->pushSingle($range) : $this->pushMulti($ranges);} else {header("Content-Length: " . $this->size);$this->readFile();}flush();}private function pushSingle($range) {$start = $end = 0;$this->getRange($range, $start, $end);header("Content-Length: " . ($end - $start + 1));header(sprintf("Content-Range: bytes %d-%d/%d", $start, $end, $this->size));fseek($this->file, $start);$this->readBuffer($end - $start + 1);$this->readFile();}private function pushMulti($ranges) {$length = $start = $end = 0;$output = "";$tl = "Content-type: application/octet-streamrn";$formatRange = "Content-range: bytes %d-%d/%drnrn";foreach ( $ranges as $range ) {
$this->getRange($range, $start, $end);$length += strlen("rn--$this->boundaryrn");$length += strlen($tl);$length += strlen(sprintf($formatRange, $start, $end, $this->size));$length += $end - $start + 1;}$length += strlen("rn--$this->boundary--rn");header("Content-Length: $length");header("Content-Type: multipart/x-byteranges; boundary=$this->boundary");foreach ( $ranges as $range ) {$this->getRange($range, $start, $end);echo "rn--$this->boundaryrn";echo $tl;echo sprintf($formatRange, $start, $end, $this->size);fseek($this->file, $start);$this->readBuffer($end - $start + 1);}echo "rn--$this->boundary--rn";}private function getRange($range, &$start, &$end) {list($start, $end) = explode('-', $range);$fileSize = $this->size;if ($start == '') {$tmp = $end;$end = $fileSize - 1;$start = $fileSize - $tmp;if ($start < 0)$start = 0;} else {if ($end == '' || $end > $fileSize - 1)$end = $fileSize - 1;}if ($start > $end) {header("Status: 416 Requested range not satisfiable");
header("Content-Range: */" . $fileSize);exit();}return array($start,$end);}private function readFile() {while ( ! feof($this->file) ) {echo fgets($this->file);flush();usleep($this->delay);}}private function readBuffer($bytes, $size = 1024) {$bytesLeft = $bytes;while ( $bytesLeft > 0 && ! feof($this->file) ) {$bytesLeft > $size ? $bytesRead = $size : $bytesRead = $bytesLeft;$bytesLeft -= $bytesRead;echo fread($this->file, $bytesRead);flush();usleep($this->delay);}}}
版权声明:本文标题:php获取远程流文件,PHP远程文件流与恢复支持 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1708825377h532132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论