php支持断点续传、分块下载的类_.docx

上传人:PIYPING 文档编号:11633595 上传时间:2021-08-27 格式:DOCX 页数:12 大小:14.06KB
返回 下载 相关 举报
php支持断点续传、分块下载的类_.docx_第1页
第1页 / 共12页
php支持断点续传、分块下载的类_.docx_第2页
第2页 / 共12页
php支持断点续传、分块下载的类_.docx_第3页
第3页 / 共12页
php支持断点续传、分块下载的类_.docx_第4页
第4页 / 共12页
php支持断点续传、分块下载的类_.docx_第5页
第5页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《php支持断点续传、分块下载的类_.docx》由会员分享,可在线阅读,更多相关《php支持断点续传、分块下载的类_.docx(12页珍藏版)》请在三一文库上搜索。

1、php支持断点续传、分块下载的类_ 本文是为大家分享php支持断点续传、分块下载的类,供大家参考,具体内容如下 ?php /* * User: djunny * Date: 2021-04-29 * Time: 17:18 * Mail: * 支持断点下载的类 */ class downloader /* * download file to local path * * param $url * param $save_file * param int $speed * param array $headers * param int $timeout * return bool * th

2、rows Exception */ static function get($url, $save_file, $speed = 10240, $headers = array(), $timeout = 10) $url_info = self:parse_url($url); if (!$url_infohost) throw new Exception(Url is Invalid); / default header $def_headers = array( Accept = */*, User-Agent = Mozilla/4.0 (compatible; MSIE 8.0; W

3、indows NT 5.1; Trident/4.0), Accept-Encoding = gzip, deflate, Host = $url_infohost, Connection = Close, Accept-Language = zh-cn, ); / merge heade $headers = array_merge($def_headers, $headers); / get content length $content_length = self:get_content_size($url_infohost, $url_infoport, $url_inforeques

4、t, $headers, $timeout); / content length not exist if (!$content_length) throw new Exception(Content-Length is Not Exists); / get exists length $exists_length = is_file($save_file) ? filesize($save_file) : 0; / get tmp data file $data_file = $save_file . .data; / get tmp data $exists_data = is_file(

5、$data_file) ? json_decode(file_get_contents($data_file), 1) : array(); / check file is valid if ($exists_length = $content_length) $exists_data unlink($data_file); return true; / check file is expire if ($exists_datalength != $content_length | $exists_length $content_length) $exists_data = array( le

6、ngth = $content_length, ); / write exists data file_put_contents($data_file, json_encode($exists_data); try $download_status = self:download_content($url_infohost, $url_infoport, $url_inforequest, $save_file, $content_length, $exists_length, $speed, $headers, $timeout); if ($download_status) unlink(

7、$data_file); catch (Exception $e) throw new Exception($e-getMessage(); return true; /* * parse url * * param $url * return bool|mixed */ static function parse_url($url) $url_info = parse_url($url); if (!$url_infohost) return false; $url_infoport = $url_infoport ? $url_infohost : 80; $url_inforequest

8、 = $url_infopath . ($url_infoquery ? ? . $url_infoquery : ); return $url_info; /* * download content by chunk * * param $host * param $port * param $url_path * param $headers * param $timeout */ static function download_content($host, $port, $url_path, $save_file, $content_length, $range_start, $spe

9、ed, $headers, $timeout) $request = self:build_header(GET, $url_path, $headers, $range_start); $fsocket = fsockopen($host, $port, $errno, $errstr, $timeout); stream_set_blocking($fsocket, TRUE); stream_set_timeout($fsocket, $timeout); fwrite($fsocket, $request); $status = stream_get_meta_data($fsocke

10、t); if ($statustimed_out) throw new Exception(Socket Connect Timeout); $is_header_end = 0; $total_size = $range_start; $file_fp = fopen($save_file, a+); while (!feof($fsocket) if (!$is_header_end) $line = fgets($fsocket); if (in_array($line, array(n, rn) $is_header_end = 1; continue; $resp = fread($

11、fsocket, $speed); $read_length = strlen($resp); if ($resp = false | $content_length $total_size + $read_length) fclose($fsocket); fclose($file_fp); throw new Exception(Socket I/O Error Or File Was Changed); $total_size += $read_length; fputs($file_fp, $resp); / check file end if ($content_length = $

12、total_size) break; sleep(1); / for test /break; fclose($fsocket); fclose($file_fp); return true; /* * get content length * * param $host * param $port * param $url_path * param $headers * param $timeout * return int */ static function get_content_size($host, $port, $url_path, $headers, $timeout) $re

13、quest = self:build_header(HEAD, $url_path, $headers); $fsocket = fsockopen($host, $port, $errno, $errstr, $timeout); stream_set_blocking($fsocket, TRUE); stream_set_timeout($fsocket, $timeout); fwrite($fsocket, $request); $status = stream_get_meta_data($fsocket); $length = 0; if ($statustimed_out) r

14、eturn 0; while (!feof($fsocket) $line = fgets($fsocket); if (in_array($line, array(n, rn) break; $line = strtolower($line); / get location if (substr($line, 0, 9) = location:) $location = trim(substr($line, 9); $url_info = self:parse_url($location); if (!$url_infohost) return 0; fclose($fsocket); re

15、turn self:get_content_size($url_infohost, $url_infoport, $url_inforequest, $headers, $timeout); / get content length if (strpos($line, content-length:) != false) list(, $length) = explode(content-length:, $line); $length = (int)trim($length); fclose($fsocket); return $length; /* * build header for s

16、ocket * * param $action * param $url_path * param $headers * param int $range_start * return string */ static function build_header($action, $url_path, $headers, $range_start = -1) $out = $action . $url_path HTTP/1.0rn; foreach ($headers as $hkey = $hval) $out .= $hkey . : . $hval . rn; if ($range_start -1) $out .= Accept-Ranges: bytesrn; $out .= Range: bytes=$range_start-rn; $out .= rn; return $out; #use age /* try if (downloader:get(http:/ test.rar) /todo echo Download Succ; catch (Exception $e) echo Download Failed; */ ? 以上就是本文的全部内容,盼望对大家的学习有所关心。 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1